graph-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/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +5 -17
- package/dist/data-structures/binary-tree/avl-tree-multi-map.js +1 -20
- package/dist/data-structures/binary-tree/avl-tree.d.ts +9 -24
- package/dist/data-structures/binary-tree/avl-tree.js +18 -34
- package/dist/data-structures/binary-tree/binary-tree.d.ts +40 -37
- package/dist/data-structures/binary-tree/binary-tree.js +68 -53
- package/dist/data-structures/binary-tree/bst.d.ts +36 -50
- package/dist/data-structures/binary-tree/bst.js +45 -60
- package/dist/data-structures/binary-tree/red-black-tree.d.ts +29 -43
- package/dist/data-structures/binary-tree/red-black-tree.js +45 -59
- package/dist/data-structures/binary-tree/tree-multi-map.d.ts +33 -45
- package/dist/data-structures/binary-tree/tree-multi-map.js +70 -83
- package/dist/interfaces/binary-tree.d.ts +4 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/bst.d.ts +3 -2
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +5 -4
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +3 -2
- package/package.json +2 -2
- 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
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type { AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType } from '../../types';
|
|
8
|
+
import type { AVLTreeMultiMapNested, AVLTreeMultiMapNodeNested, AVLTreeMultiMapOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback, IterationType } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
11
11
|
export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNodeNested<K, V>> extends AVLTreeNode<K, V, NODE> {
|
|
@@ -20,23 +20,11 @@ export declare class AVLTreeMultiMapNode<K = any, V = any, NODE extends AVLTreeM
|
|
|
20
20
|
* parameter when creating a new instance of the `BinaryTreeNode` class.
|
|
21
21
|
*/
|
|
22
22
|
constructor(key: K, value?: V, count?: number);
|
|
23
|
-
protected _count: number;
|
|
24
|
-
/**
|
|
25
|
-
* The function returns the value of the protected variable _count.
|
|
26
|
-
* @returns The count property of the object, which is of type number.
|
|
27
|
-
*/
|
|
28
|
-
get count(): number;
|
|
29
|
-
/**
|
|
30
|
-
* The above function sets the value of the count property.
|
|
31
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
32
|
-
* numeric value.
|
|
33
|
-
*/
|
|
34
|
-
set count(value: number);
|
|
35
23
|
}
|
|
36
24
|
/**
|
|
37
25
|
* The only distinction between a AVLTreeMultiMap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
38
26
|
*/
|
|
39
|
-
export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>> extends AVLTree<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
27
|
+
export declare class AVLTreeMultiMap<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends AVLTreeMultiMapNode<K, V, NODE> = AVLTreeMultiMapNode<K, V, AVLTreeMultiMapNodeNested<K, V>>, TREE extends AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, TREE> = AVLTreeMultiMap<K, V, R, MK, MV, MR, NODE, AVLTreeMultiMapNested<K, V, R, MK, MV, MR, NODE>>> extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
40
28
|
/**
|
|
41
29
|
* The constructor initializes a new AVLTreeMultiMap object with optional initial elements.
|
|
42
30
|
* @param keysNodesEntriesOrRaws - The `keysNodesEntriesOrRaws` parameter is an
|
|
@@ -81,7 +69,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
81
69
|
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
82
70
|
* object.
|
|
83
71
|
*/
|
|
84
|
-
createTree(options?: AVLTreeMultiMapOptions<K, V, R>):
|
|
72
|
+
createTree(options?: AVLTreeMultiMapOptions<K, V, R>): TREE;
|
|
85
73
|
/**
|
|
86
74
|
* The function checks if the input is an instance of AVLTreeMultiMapNode.
|
|
87
75
|
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
@@ -156,7 +144,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
156
144
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
157
145
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
158
146
|
*/
|
|
159
|
-
clone():
|
|
147
|
+
clone(): TREE;
|
|
160
148
|
/**
|
|
161
149
|
* The `map` function in TypeScript overrides the default behavior to create a new AVLTreeMultiMap
|
|
162
150
|
* with modified entries based on a provided callback.
|
|
@@ -175,7 +163,7 @@ export declare class AVLTreeMultiMap<K = any, V = any, R = object, NODE extends
|
|
|
175
163
|
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
176
164
|
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
177
165
|
*/
|
|
178
|
-
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeMultiMapOptions<MK, MV, MR>, thisArg?: any): AVLTreeMultiMap<MK, MV, MR
|
|
166
|
+
map<MK, MV, MR>(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeMultiMapOptions<MK, MV, MR>, thisArg?: any): AVLTreeMultiMap<MK, MV, MR>;
|
|
179
167
|
/**
|
|
180
168
|
* The function `keyValueNodeEntryRawToNodeAndValue` converts a key, value, entry, or raw element into
|
|
181
169
|
* a node object.
|
|
@@ -15,24 +15,8 @@ class AVLTreeMultiMapNode extends avl_tree_1.AVLTreeNode {
|
|
|
15
15
|
*/
|
|
16
16
|
constructor(key, value, count = 1) {
|
|
17
17
|
super(key, value);
|
|
18
|
-
this._count = 1;
|
|
19
18
|
this.count = count;
|
|
20
19
|
}
|
|
21
|
-
/**
|
|
22
|
-
* The function returns the value of the protected variable _count.
|
|
23
|
-
* @returns The count property of the object, which is of type number.
|
|
24
|
-
*/
|
|
25
|
-
get count() {
|
|
26
|
-
return this._count;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* The above function sets the value of the count property.
|
|
30
|
-
* @param {number} value - The value parameter is of type number, which means it can accept any
|
|
31
|
-
* numeric value.
|
|
32
|
-
*/
|
|
33
|
-
set count(value) {
|
|
34
|
-
this._count = value;
|
|
35
|
-
}
|
|
36
20
|
}
|
|
37
21
|
exports.AVLTreeMultiMapNode = AVLTreeMultiMapNode;
|
|
38
22
|
/**
|
|
@@ -95,7 +79,6 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
95
79
|
* @returns a new instance of the AVLTreeMultiMap class, with the specified options, as a TREE
|
|
96
80
|
* object.
|
|
97
81
|
*/
|
|
98
|
-
// @ts-ignore
|
|
99
82
|
createTree(options) {
|
|
100
83
|
return new AVLTreeMultiMap([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
101
84
|
}
|
|
@@ -173,7 +156,7 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
173
156
|
else {
|
|
174
157
|
if (!curr.left) {
|
|
175
158
|
if (!parent) {
|
|
176
|
-
if (curr.right !== undefined)
|
|
159
|
+
if (curr.right !== undefined && curr.right !== null)
|
|
177
160
|
this._setRoot(curr.right);
|
|
178
161
|
}
|
|
179
162
|
else {
|
|
@@ -286,7 +269,6 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
286
269
|
* The function overrides the clone method to create a deep copy of a tree object.
|
|
287
270
|
* @returns The `clone()` method is returning a cloned instance of the `TREE` object.
|
|
288
271
|
*/
|
|
289
|
-
// @ts-ignore
|
|
290
272
|
clone() {
|
|
291
273
|
const cloned = this.createTree();
|
|
292
274
|
if (this._isMapMode)
|
|
@@ -315,7 +297,6 @@ class AVLTreeMultiMap extends avl_tree_1.AVLTree {
|
|
|
315
297
|
* `callback` function along with the index and the original tree itself. The transformed entries are
|
|
316
298
|
* then added to the new `AVLTreeMultiMap` instance, which is returned at the end.
|
|
317
299
|
*/
|
|
318
|
-
// @ts-ignore
|
|
319
300
|
map(callback, options, thisArg) {
|
|
320
301
|
const newTree = new AVLTreeMultiMap([], options);
|
|
321
302
|
let index = 0;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback } from '../../types';
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeleteResult, BSTNOptKeyOrNode, BTNRep, EntryCallback } 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
|
/**
|
|
@@ -18,18 +18,6 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
18
18
|
* value associated with the key in the constructor.
|
|
19
19
|
*/
|
|
20
20
|
constructor(key: K, value?: V);
|
|
21
|
-
protected _height: number;
|
|
22
|
-
/**
|
|
23
|
-
* The function returns the value of the height property.
|
|
24
|
-
* @returns The height of the object.
|
|
25
|
-
*/
|
|
26
|
-
get height(): number;
|
|
27
|
-
/**
|
|
28
|
-
* The above function sets the value of the height property.
|
|
29
|
-
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
30
|
-
* set.
|
|
31
|
-
*/
|
|
32
|
-
set height(value: number);
|
|
33
21
|
}
|
|
34
22
|
/**
|
|
35
23
|
* 1. Height-Balanced: Each node's left and right subtrees differ in height by no more than one.
|
|
@@ -40,7 +28,7 @@ export declare class AVLTreeNode<K = any, V = any, NODE extends AVLTreeNode<K, V
|
|
|
40
28
|
* 6. Complex Insertions and Deletions: Due to rebalancing, these operations are more complex than in a regular BST.
|
|
41
29
|
* 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
30
|
*/
|
|
43
|
-
export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>> extends BST<K, V, R, NODE> implements IBinaryTree<K, V, R, NODE> {
|
|
31
|
+
export declare class AVLTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends AVLTreeNode<K, V, NODE> = AVLTreeNode<K, V, AVLTreeNodeNested<K, V>>, TREE extends AVLTree<K, V, R, MK, MV, MR, NODE, TREE> = AVLTree<K, V, R, MK, MV, MR, NODE, AVLTreeNested<K, V, R, MK, MV, MR, NODE>>> extends BST<K, V, R, MK, MV, MR, NODE, TREE> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
44
32
|
/**
|
|
45
33
|
* This is a constructor function for an AVLTree class that initializes the tree with keys, nodes,
|
|
46
34
|
* entries, or raw elements.
|
|
@@ -64,16 +52,13 @@ export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeN
|
|
|
64
52
|
*/
|
|
65
53
|
createNode(key: K, value?: V): NODE;
|
|
66
54
|
/**
|
|
67
|
-
* The function
|
|
68
|
-
* options
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* creates a
|
|
73
|
-
* @returns An AVLTree object is being returned with the specified options and properties inherited
|
|
74
|
-
* from the current object.
|
|
55
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
56
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
57
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
58
|
+
* being created.
|
|
59
|
+
* @returns a new AVLTree object.
|
|
75
60
|
*/
|
|
76
|
-
createTree(options?: AVLTreeOptions<K, V, R>):
|
|
61
|
+
createTree(options?: AVLTreeOptions<K, V, R>): TREE;
|
|
77
62
|
/**
|
|
78
63
|
* The function checks if the input is an instance of AVLTreeNode.
|
|
79
64
|
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The parameter
|
|
@@ -110,7 +95,7 @@ export declare class AVLTree<K = any, V = any, R = object, NODE extends AVLTreeN
|
|
|
110
95
|
* `deletedResults`, which is an array of `BinaryTreeDeleteResult` objects.
|
|
111
96
|
*/
|
|
112
97
|
delete(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R): BinaryTreeDeleteResult<NODE>[];
|
|
113
|
-
map
|
|
98
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: AVLTreeOptions<MK, MV, MR>, thisArg?: any): AVLTree<MK, MV, MR>;
|
|
114
99
|
/**
|
|
115
100
|
* Time Complexity: O(1)
|
|
116
101
|
* Space Complexity: O(1)
|
|
@@ -20,22 +20,6 @@ class AVLTreeNode extends bst_1.BSTNode {
|
|
|
20
20
|
*/
|
|
21
21
|
constructor(key, value) {
|
|
22
22
|
super(key, value);
|
|
23
|
-
this._height = 0;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* The function returns the value of the height property.
|
|
27
|
-
* @returns The height of the object.
|
|
28
|
-
*/
|
|
29
|
-
get height() {
|
|
30
|
-
return this._height;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* The above function sets the value of the height property.
|
|
34
|
-
* @param {number} value - The value parameter is a number that represents the new height value to be
|
|
35
|
-
* set.
|
|
36
|
-
*/
|
|
37
|
-
set height(value) {
|
|
38
|
-
this._height = value;
|
|
39
23
|
}
|
|
40
24
|
}
|
|
41
25
|
exports.AVLTreeNode = AVLTreeNode;
|
|
@@ -78,16 +62,12 @@ class AVLTree extends bst_1.BST {
|
|
|
78
62
|
return new AVLTreeNode(key, this._isMapMode ? undefined : value);
|
|
79
63
|
}
|
|
80
64
|
/**
|
|
81
|
-
* The function
|
|
82
|
-
* options
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* creates a
|
|
87
|
-
* @returns An AVLTree object is being returned with the specified options and properties inherited
|
|
88
|
-
* from the current object.
|
|
65
|
+
* The function creates a new AVL tree with the specified options and returns it.
|
|
66
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
67
|
+
* passed to the `createTree` function. It is used to customize the behavior of the AVL tree that is
|
|
68
|
+
* being created.
|
|
69
|
+
* @returns a new AVLTree object.
|
|
89
70
|
*/
|
|
90
|
-
// @ts-ignore
|
|
91
71
|
createTree(options) {
|
|
92
72
|
return new AVLTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, specifyComparable: this._specifyComparable, toEntryFn: this._toEntryFn, isReverse: this._isReverse }, options));
|
|
93
73
|
}
|
|
@@ -144,7 +124,6 @@ class AVLTree extends bst_1.BST {
|
|
|
144
124
|
}
|
|
145
125
|
return deletedResults;
|
|
146
126
|
}
|
|
147
|
-
// @ts-ignore
|
|
148
127
|
map(callback, options, thisArg) {
|
|
149
128
|
const newTree = new AVLTree([], options);
|
|
150
129
|
let index = 0;
|
|
@@ -237,7 +216,8 @@ class AVLTree extends bst_1.BST {
|
|
|
237
216
|
_balanceLL(A) {
|
|
238
217
|
const parentOfA = A.parent;
|
|
239
218
|
const B = A.left;
|
|
240
|
-
|
|
219
|
+
if (B !== null)
|
|
220
|
+
A.parent = B;
|
|
241
221
|
if (B && B.right) {
|
|
242
222
|
B.right.parent = A;
|
|
243
223
|
}
|
|
@@ -278,13 +258,14 @@ class AVLTree extends bst_1.BST {
|
|
|
278
258
|
if (B) {
|
|
279
259
|
C = B.right;
|
|
280
260
|
}
|
|
281
|
-
if (A)
|
|
261
|
+
if (A && C !== null)
|
|
282
262
|
A.parent = C;
|
|
283
|
-
if (B)
|
|
263
|
+
if (B && C !== null)
|
|
284
264
|
B.parent = C;
|
|
285
265
|
if (C) {
|
|
286
266
|
if (C.left) {
|
|
287
|
-
|
|
267
|
+
if (B !== null)
|
|
268
|
+
C.left.parent = B;
|
|
288
269
|
}
|
|
289
270
|
if (C.right) {
|
|
290
271
|
C.right.parent = A;
|
|
@@ -328,7 +309,8 @@ class AVLTree extends bst_1.BST {
|
|
|
328
309
|
_balanceRR(A) {
|
|
329
310
|
const parentOfA = A.parent;
|
|
330
311
|
const B = A.right;
|
|
331
|
-
|
|
312
|
+
if (B !== null)
|
|
313
|
+
A.parent = B;
|
|
332
314
|
if (B) {
|
|
333
315
|
if (B.left) {
|
|
334
316
|
B.left.parent = A;
|
|
@@ -371,15 +353,17 @@ class AVLTree extends bst_1.BST {
|
|
|
371
353
|
if (B) {
|
|
372
354
|
C = B.left;
|
|
373
355
|
}
|
|
374
|
-
|
|
375
|
-
|
|
356
|
+
if (C !== null)
|
|
357
|
+
A.parent = C;
|
|
358
|
+
if (B && C !== null)
|
|
376
359
|
B.parent = C;
|
|
377
360
|
if (C) {
|
|
378
361
|
if (C.left) {
|
|
379
362
|
C.left.parent = A;
|
|
380
363
|
}
|
|
381
364
|
if (C.right) {
|
|
382
|
-
|
|
365
|
+
if (B !== null)
|
|
366
|
+
C.right.parent = B;
|
|
383
367
|
}
|
|
384
368
|
C.parent = parentOfA;
|
|
385
369
|
}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BinaryTreeDeleteResult, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, ToEntryFn } from '../../types';
|
|
8
|
+
import { BinaryTreeDeleteResult, BinaryTreeNested, BinaryTreeNodeNested, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, BTNRep, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, OptNodeOrNull, type RBTNColor, ToEntryFn } from '../../types';
|
|
9
9
|
import { IBinaryTree } from '../../interfaces';
|
|
10
10
|
import { IterableEntryBase } from '../base';
|
|
11
11
|
import { Range } from '../../common';
|
|
@@ -19,12 +19,21 @@ export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNod
|
|
|
19
19
|
value?: V;
|
|
20
20
|
parent?: NODE;
|
|
21
21
|
constructor(key: K, value?: V);
|
|
22
|
-
|
|
22
|
+
_left?: OptNodeOrNull<NODE>;
|
|
23
23
|
get left(): OptNodeOrNull<NODE>;
|
|
24
24
|
set left(v: OptNodeOrNull<NODE>);
|
|
25
|
-
|
|
25
|
+
_right?: OptNodeOrNull<NODE>;
|
|
26
26
|
get right(): OptNodeOrNull<NODE>;
|
|
27
27
|
set right(v: OptNodeOrNull<NODE>);
|
|
28
|
+
_height: number;
|
|
29
|
+
get height(): number;
|
|
30
|
+
set height(value: number);
|
|
31
|
+
_color: RBTNColor;
|
|
32
|
+
get color(): RBTNColor;
|
|
33
|
+
set color(value: RBTNColor);
|
|
34
|
+
_count: number;
|
|
35
|
+
get count(): number;
|
|
36
|
+
set count(value: number);
|
|
28
37
|
get familyPosition(): FamilyPosition;
|
|
29
38
|
}
|
|
30
39
|
/**
|
|
@@ -34,7 +43,7 @@ export declare class BinaryTreeNode<K = any, V = any, NODE extends BinaryTreeNod
|
|
|
34
43
|
* 4. Subtrees: Each child of a node forms the root of a subtree.
|
|
35
44
|
* 5. Leaf Nodes: Nodes without children are leaves.
|
|
36
45
|
*/
|
|
37
|
-
export declare class BinaryTree<K = any, V = any, R = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, NODE> {
|
|
46
|
+
export declare class BinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object, NODE extends BinaryTreeNode<K, V, NODE> = BinaryTreeNode<K, V, BinaryTreeNodeNested<K, V>>, TREE extends BinaryTree<K, V, R, MK, MV, MR, NODE, TREE> = BinaryTree<K, V, R, MK, MV, MR, NODE, BinaryTreeNested<K, V, R, MK, MV, MR, NODE>>> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R, MK, MV, MR, NODE, TREE> {
|
|
38
47
|
iterationType: IterationType;
|
|
39
48
|
/**
|
|
40
49
|
* The constructor initializes a binary tree with optional options and adds keys, nodes, entries, or
|
|
@@ -72,20 +81,30 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
72
81
|
*/
|
|
73
82
|
createNode(key: K, value?: V): NODE;
|
|
74
83
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
* binary tree, such as the iteration type, whether the tree is in map mode, and functions for
|
|
82
|
-
* converting entries.
|
|
83
|
-
* @returns The `createTree` method is returning an instance of the `BinaryTree` class with the
|
|
84
|
-
* provided options. The method is creating a new `BinaryTree` object with an empty array as the
|
|
85
|
-
* initial data, and then setting various options such as `iterationType`, `isMapMode`, and
|
|
86
|
-
* `toEntryFn` based on the current object's properties and the provided `options`. Finally, it
|
|
84
|
+
* The function creates a binary tree with the specified options.
|
|
85
|
+
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
86
|
+
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
87
|
+
* `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
|
|
88
|
+
* of properties
|
|
89
|
+
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
87
90
|
*/
|
|
88
|
-
createTree(options?: BinaryTreeOptions<K, V, R>):
|
|
91
|
+
createTree(options?: BinaryTreeOptions<K, V, R>): TREE;
|
|
92
|
+
/**
|
|
93
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
94
|
+
* or returns null.
|
|
95
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
96
|
+
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
97
|
+
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
98
|
+
* node, an entry
|
|
99
|
+
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
100
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
101
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
102
|
+
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
103
|
+
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
104
|
+
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
105
|
+
* value.
|
|
106
|
+
*/
|
|
107
|
+
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNodeOrNull<NODE>, V | undefined];
|
|
89
108
|
/**
|
|
90
109
|
* Time Complexity: O(n)
|
|
91
110
|
* Space Complexity: O(log n)
|
|
@@ -231,7 +250,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
231
250
|
* elements from the other tree.
|
|
232
251
|
* @param anotherTree - `BinaryTree<K, V, R, NODE, TREE>`
|
|
233
252
|
*/
|
|
234
|
-
merge(anotherTree:
|
|
253
|
+
merge(anotherTree: BinaryTree<K, V, R, NODE, TREE>): void;
|
|
235
254
|
/**
|
|
236
255
|
* Time Complexity: O(k * n)
|
|
237
256
|
* Space Complexity: O(1)
|
|
@@ -631,7 +650,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
631
650
|
* original tree using breadth-first search (bfs), and adds the nodes to the new tree. If a node in
|
|
632
651
|
* the original tree is null, a null node is added to the cloned tree. If a node
|
|
633
652
|
*/
|
|
634
|
-
clone():
|
|
653
|
+
clone(): TREE;
|
|
635
654
|
/**
|
|
636
655
|
* Time Complexity: O(n)
|
|
637
656
|
* Space Complexity: O(n)
|
|
@@ -648,7 +667,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
648
667
|
* @returns The `filter` method is returning a new tree that contains entries that pass the provided
|
|
649
668
|
* predicate function.
|
|
650
669
|
*/
|
|
651
|
-
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any):
|
|
670
|
+
filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: any): TREE;
|
|
652
671
|
/**
|
|
653
672
|
* Time Complexity: O(n)
|
|
654
673
|
* Space Complexity: O(n)
|
|
@@ -668,7 +687,7 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
668
687
|
* @returns The `map` function is returning a new `BinaryTree` instance filled with entries that are
|
|
669
688
|
* the result of applying the provided `callback` function to each entry in the original tree.
|
|
670
689
|
*/
|
|
671
|
-
map
|
|
690
|
+
map(callback: EntryCallback<K, V | undefined, [MK, MV]>, options?: BinaryTreeOptions<MK, MV, MR>, thisArg?: any): BinaryTree<MK, MV, MR>;
|
|
672
691
|
/**
|
|
673
692
|
* Time Complexity: O(n)
|
|
674
693
|
* Space Complexity: O(n)
|
|
@@ -704,22 +723,6 @@ export declare class BinaryTree<K = any, V = any, R = object, NODE extends Binar
|
|
|
704
723
|
* provided, the default value is set to
|
|
705
724
|
*/
|
|
706
725
|
print(options?: BinaryTreePrintOptions, startNode?: BTNRep<K, V, NODE> | R): void;
|
|
707
|
-
/**
|
|
708
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
709
|
-
* or returns null.
|
|
710
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
711
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
712
|
-
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
713
|
-
* node, an entry
|
|
714
|
-
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
715
|
-
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
716
|
-
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
717
|
-
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
718
|
-
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
719
|
-
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
720
|
-
* value.
|
|
721
|
-
*/
|
|
722
|
-
protected _keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw: BTNRep<K, V, NODE> | R, value?: V): [OptNodeOrNull<NODE>, V | undefined];
|
|
723
726
|
/**
|
|
724
727
|
* Time complexity: O(n)
|
|
725
728
|
* Space complexity: O(n)
|
|
@@ -19,6 +19,9 @@ const common_1 = require("../../common");
|
|
|
19
19
|
*/
|
|
20
20
|
class BinaryTreeNode {
|
|
21
21
|
constructor(key, value) {
|
|
22
|
+
this._height = 0;
|
|
23
|
+
this._color = 'BLACK';
|
|
24
|
+
this._count = 1;
|
|
22
25
|
this.key = key;
|
|
23
26
|
this.value = value;
|
|
24
27
|
}
|
|
@@ -40,6 +43,24 @@ class BinaryTreeNode {
|
|
|
40
43
|
}
|
|
41
44
|
this._right = v;
|
|
42
45
|
}
|
|
46
|
+
get height() {
|
|
47
|
+
return this._height;
|
|
48
|
+
}
|
|
49
|
+
set height(value) {
|
|
50
|
+
this._height = value;
|
|
51
|
+
}
|
|
52
|
+
get color() {
|
|
53
|
+
return this._color;
|
|
54
|
+
}
|
|
55
|
+
set color(value) {
|
|
56
|
+
this._color = value;
|
|
57
|
+
}
|
|
58
|
+
get count() {
|
|
59
|
+
return this._count;
|
|
60
|
+
}
|
|
61
|
+
set count(value) {
|
|
62
|
+
this._count = value;
|
|
63
|
+
}
|
|
43
64
|
get familyPosition() {
|
|
44
65
|
const that = this;
|
|
45
66
|
if (!this.parent) {
|
|
@@ -128,22 +149,57 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
128
149
|
return new BinaryTreeNode(key, this._isMapMode ? undefined : value);
|
|
129
150
|
}
|
|
130
151
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
* binary tree, such as the iteration type, whether the tree is in map mode, and functions for
|
|
138
|
-
* converting entries.
|
|
139
|
-
* @returns The `createTree` method is returning an instance of the `BinaryTree` class with the
|
|
140
|
-
* provided options. The method is creating a new `BinaryTree` object with an empty array as the
|
|
141
|
-
* initial data, and then setting various options such as `iterationType`, `isMapMode`, and
|
|
142
|
-
* `toEntryFn` based on the current object's properties and the provided `options`. Finally, it
|
|
152
|
+
* The function creates a binary tree with the specified options.
|
|
153
|
+
* @param [options] - The `options` parameter in the `createTree` function is an optional parameter
|
|
154
|
+
* that allows you to provide partial configuration options for creating a binary tree. It is of type
|
|
155
|
+
* `Partial<BinaryTreeOptions<K, V, R>>`, which means you can pass in an object containing a subset
|
|
156
|
+
* of properties
|
|
157
|
+
* @returns A new instance of a binary tree with the specified options is being returned.
|
|
143
158
|
*/
|
|
144
159
|
createTree(options) {
|
|
145
160
|
return new BinaryTree([], Object.assign({ iterationType: this.iterationType, isMapMode: this._isMapMode, toEntryFn: this._toEntryFn }, options));
|
|
146
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
164
|
+
* or returns null.
|
|
165
|
+
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
166
|
+
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
167
|
+
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
168
|
+
* node, an entry
|
|
169
|
+
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
170
|
+
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
171
|
+
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
172
|
+
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
173
|
+
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
174
|
+
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
175
|
+
* value.
|
|
176
|
+
*/
|
|
177
|
+
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
178
|
+
if (keyNodeEntryOrRaw === undefined)
|
|
179
|
+
return [undefined, undefined];
|
|
180
|
+
if (keyNodeEntryOrRaw === null)
|
|
181
|
+
return [null, undefined];
|
|
182
|
+
if (this.isNode(keyNodeEntryOrRaw))
|
|
183
|
+
return [keyNodeEntryOrRaw, value];
|
|
184
|
+
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
185
|
+
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
186
|
+
if (key === undefined)
|
|
187
|
+
return [undefined, undefined];
|
|
188
|
+
else if (key === null)
|
|
189
|
+
return [null, undefined];
|
|
190
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
191
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
192
|
+
}
|
|
193
|
+
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
194
|
+
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
195
|
+
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
196
|
+
if (this.isKey(key))
|
|
197
|
+
return [this.createNode(key, finalValue), finalValue];
|
|
198
|
+
}
|
|
199
|
+
if (this.isKey(keyNodeEntryOrRaw))
|
|
200
|
+
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
201
|
+
return [undefined, undefined];
|
|
202
|
+
}
|
|
147
203
|
/**
|
|
148
204
|
* Time Complexity: O(n)
|
|
149
205
|
* Space Complexity: O(log n)
|
|
@@ -1559,47 +1615,6 @@ class BinaryTree extends base_1.IterableEntryBase {
|
|
|
1559
1615
|
print(options, startNode = this._root) {
|
|
1560
1616
|
console.log(this.toVisual(startNode, options));
|
|
1561
1617
|
}
|
|
1562
|
-
/**
|
|
1563
|
-
* The function `keyValueNodeEntryRawToNodeAndValue` converts various input types into a node object
|
|
1564
|
-
* or returns null.
|
|
1565
|
-
* @param {BTNRep<K, V, NODE> | R} keyNodeEntryOrRaw - The
|
|
1566
|
-
* `keyValueNodeEntryRawToNodeAndValue` function takes in a parameter `keyNodeEntryOrRaw`, which
|
|
1567
|
-
* can be of type `BTNRep<K, V, NODE>` or `R`. This parameter represents either a key, a
|
|
1568
|
-
* node, an entry
|
|
1569
|
-
* @param {V} [value] - The `value` parameter in the `keyValueNodeEntryRawToNodeAndValue` function is
|
|
1570
|
-
* an optional parameter of type `V`. It represents the value associated with the key in the node
|
|
1571
|
-
* being created. If a `value` is provided, it will be used when creating the node. If
|
|
1572
|
-
* @returns The `keyValueNodeEntryRawToNodeAndValue` function returns an optional node
|
|
1573
|
-
* (`OptNodeOrNull<NODE>`) based on the input parameters provided. The function checks the type of the
|
|
1574
|
-
* input parameter (`keyNodeEntryOrRaw`) and processes it accordingly to return a node or null
|
|
1575
|
-
* value.
|
|
1576
|
-
*/
|
|
1577
|
-
_keyValueNodeEntryRawToNodeAndValue(keyNodeEntryOrRaw, value) {
|
|
1578
|
-
if (keyNodeEntryOrRaw === undefined)
|
|
1579
|
-
return [undefined, undefined];
|
|
1580
|
-
if (keyNodeEntryOrRaw === null)
|
|
1581
|
-
return [null, undefined];
|
|
1582
|
-
if (this.isNode(keyNodeEntryOrRaw))
|
|
1583
|
-
return [keyNodeEntryOrRaw, value];
|
|
1584
|
-
if (this.isEntry(keyNodeEntryOrRaw)) {
|
|
1585
|
-
const [key, entryValue] = keyNodeEntryOrRaw;
|
|
1586
|
-
if (key === undefined)
|
|
1587
|
-
return [undefined, undefined];
|
|
1588
|
-
else if (key === null)
|
|
1589
|
-
return [null, undefined];
|
|
1590
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1591
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
1592
|
-
}
|
|
1593
|
-
if (this.isRaw(keyNodeEntryOrRaw)) {
|
|
1594
|
-
const [key, entryValue] = this._toEntryFn(keyNodeEntryOrRaw);
|
|
1595
|
-
const finalValue = value !== null && value !== void 0 ? value : entryValue;
|
|
1596
|
-
if (this.isKey(key))
|
|
1597
|
-
return [this.createNode(key, finalValue), finalValue];
|
|
1598
|
-
}
|
|
1599
|
-
if (this.isKey(keyNodeEntryOrRaw))
|
|
1600
|
-
return [this.createNode(keyNodeEntryOrRaw, value), value];
|
|
1601
|
-
return [undefined, undefined];
|
|
1602
|
-
}
|
|
1603
1618
|
/**
|
|
1604
1619
|
* Time complexity: O(n)
|
|
1605
1620
|
* Space complexity: O(n)
|