linked-list-typed 1.46.8 → 1.47.1
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.d.ts +4 -2
- package/dist/data-structures/binary-tree/avl-tree.js +10 -0
- package/dist/data-structures/binary-tree/binary-tree.d.ts +51 -13
- package/dist/data-structures/binary-tree/binary-tree.js +153 -55
- package/dist/data-structures/binary-tree/bst.d.ts +10 -9
- package/dist/data-structures/binary-tree/bst.js +16 -14
- package/dist/data-structures/binary-tree/rb-tree.d.ts +7 -5
- package/dist/data-structures/binary-tree/rb-tree.js +32 -23
- package/dist/data-structures/binary-tree/tree-multimap.d.ts +5 -3
- package/dist/data-structures/binary-tree/tree-multimap.js +11 -2
- package/dist/interfaces/binary-tree.d.ts +3 -3
- package/dist/types/common.d.ts +5 -4
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/binary-tree.d.ts +3 -1
- package/dist/types/data-structures/binary-tree/binary-tree.js +0 -6
- package/dist/types/data-structures/binary-tree/bst.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/rb-tree.d.ts +2 -1
- package/dist/types/data-structures/binary-tree/tree-multimap.d.ts +2 -1
- package/package.json +2 -2
- package/src/data-structures/binary-tree/avl-tree.ts +17 -5
- package/src/data-structures/binary-tree/binary-tree.ts +175 -62
- package/src/data-structures/binary-tree/bst.ts +23 -19
- package/src/data-structures/binary-tree/rb-tree.ts +38 -27
- package/src/data-structures/binary-tree/tree-multimap.ts +19 -6
- package/src/interfaces/binary-tree.ts +3 -3
- package/src/types/common.ts +2 -5
- package/src/types/data-structures/binary-tree/avl-tree.ts +5 -1
- package/src/types/data-structures/binary-tree/binary-tree.ts +5 -7
- package/src/types/data-structures/binary-tree/bst.ts +3 -1
- package/src/types/data-structures/binary-tree/rb-tree.ts +3 -1
- package/src/types/data-structures/binary-tree/tree-multimap.ts +3 -1
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import type {
|
|
8
|
+
import type { BSTNested, BSTNodeNested, BSTOptions, BTNCallback, BTNKey } from '../../types';
|
|
9
9
|
import { CP, IterationType } from '../../types';
|
|
10
10
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -33,7 +33,8 @@ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>
|
|
|
33
33
|
*/
|
|
34
34
|
set right(v: N | undefined);
|
|
35
35
|
}
|
|
36
|
-
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
|
|
36
|
+
export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>, TREE extends BST<V, N, TREE> = BST<V, N, BSTNested<V, N>>> extends BinaryTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
37
|
+
options: BSTOptions;
|
|
37
38
|
/**
|
|
38
39
|
* The constructor function initializes a binary search tree with an optional comparator function.
|
|
39
40
|
* @param {BSTOptions} [options] - An optional object that contains additional configuration options
|
|
@@ -54,6 +55,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
54
55
|
* @returns a new instance of the BSTNode class with the specified key and value.
|
|
55
56
|
*/
|
|
56
57
|
createNode(key: BTNKey, value?: V): N;
|
|
58
|
+
createTree(options?: BSTOptions): TREE;
|
|
57
59
|
/**
|
|
58
60
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
59
61
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -95,7 +97,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
95
97
|
* current instance of the binary search tree
|
|
96
98
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
97
99
|
*/
|
|
98
|
-
addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | undefined)[];
|
|
100
|
+
addMany(keysOrNodes: (BTNKey | N | undefined)[], data?: (V | undefined)[], isBalanceAdd?: boolean, iterationType?: IterationType | undefined): (N | undefined)[];
|
|
99
101
|
/**
|
|
100
102
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
101
103
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -115,7 +117,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
115
117
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
116
118
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
117
119
|
*/
|
|
118
|
-
lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): BTNKey;
|
|
120
|
+
lastKey(beginRoot?: BTNKey | N | undefined, iterationType?: IterationType | undefined): BTNKey;
|
|
119
121
|
/**
|
|
120
122
|
* Time Complexity: O(log n) - Average case for a balanced tree.
|
|
121
123
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -172,7 +174,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
172
174
|
* performed on the binary tree. It can have two possible values:
|
|
173
175
|
* @returns The method returns an array of nodes (`N[]`).
|
|
174
176
|
*/
|
|
175
|
-
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType): N[];
|
|
177
|
+
getNodes<C extends BTNCallback<N>>(identifier: ReturnType<C> | undefined, callback?: C, onlyOne?: boolean, beginRoot?: BTNKey | N | undefined, iterationType?: IterationType | undefined): N[];
|
|
176
178
|
/**
|
|
177
179
|
* Time Complexity: O(log n) - Average case for a balanced tree. O(n) - Visiting each node once when identifier is not node's key.
|
|
178
180
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -198,7 +200,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
198
200
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
199
201
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
200
202
|
*/
|
|
201
|
-
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType): ReturnType<C>[];
|
|
203
|
+
lesserOrGreaterTraverse<C extends BTNCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BTNKey | N | undefined, iterationType?: IterationType | undefined): ReturnType<C>[];
|
|
202
204
|
/**
|
|
203
205
|
* Balancing Adjustment:
|
|
204
206
|
* Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
|
|
@@ -223,7 +225,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
223
225
|
* values:
|
|
224
226
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
225
227
|
*/
|
|
226
|
-
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
228
|
+
perfectlyBalance(iterationType?: IterationType | undefined): boolean;
|
|
227
229
|
/**
|
|
228
230
|
* Time Complexity: O(n) - Visiting each node once.
|
|
229
231
|
* Space Complexity: O(log n) - Space for the recursive call stack in the worst case.
|
|
@@ -237,8 +239,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
|
|
|
237
239
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
238
240
|
* @returns a boolean value.
|
|
239
241
|
*/
|
|
240
|
-
isAVLBalanced(iterationType?: IterationType): boolean;
|
|
241
|
-
protected _comparator: BSTComparator;
|
|
242
|
+
isAVLBalanced(iterationType?: IterationType | undefined): boolean;
|
|
242
243
|
protected _setRoot(v: N | undefined): void;
|
|
243
244
|
/**
|
|
244
245
|
* The function compares two values using a comparator function and returns whether the first value
|
|
@@ -53,14 +53,13 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
53
53
|
*/
|
|
54
54
|
constructor(options) {
|
|
55
55
|
super(options);
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this._comparator = comparator;
|
|
62
|
-
}
|
|
56
|
+
if (options) {
|
|
57
|
+
this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b }, options);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
63
61
|
}
|
|
62
|
+
this._root = undefined;
|
|
64
63
|
}
|
|
65
64
|
/**
|
|
66
65
|
* Get the root node of the binary tree.
|
|
@@ -79,6 +78,9 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
79
78
|
createNode(key, value) {
|
|
80
79
|
return new BSTNode(key, value);
|
|
81
80
|
}
|
|
81
|
+
createTree(options) {
|
|
82
|
+
return new BST(Object.assign(Object.assign({}, this.options), options));
|
|
83
|
+
}
|
|
82
84
|
/**
|
|
83
85
|
* Time Complexity: O(log n) - Average case for a balanced tree. In the worst case (unbalanced tree), it can be O(n).
|
|
84
86
|
* Space Complexity: O(1) - Constant space is used.
|
|
@@ -196,7 +198,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
196
198
|
* current instance of the binary search tree
|
|
197
199
|
* @returns The function `addMany` returns an array of nodes (`N`) or `undefined` values.
|
|
198
200
|
*/
|
|
199
|
-
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
201
|
+
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.options.iterationType) {
|
|
200
202
|
// TODO this addMany function is inefficient, it should be optimized
|
|
201
203
|
function hasNoUndefined(arr) {
|
|
202
204
|
return arr.indexOf(undefined) === -1;
|
|
@@ -284,7 +286,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
284
286
|
* the key of the leftmost node if the comparison result is greater than, and the key of the
|
|
285
287
|
* rightmost node otherwise. If no node is found, it returns 0.
|
|
286
288
|
*/
|
|
287
|
-
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
289
|
+
lastKey(beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
288
290
|
var _a, _b, _c, _d, _e, _f;
|
|
289
291
|
if (this._compare(0, 1) === types_1.CP.lt)
|
|
290
292
|
return (_b = (_a = this.getRightMost(beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
|
|
@@ -381,7 +383,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
381
383
|
* performed on the binary tree. It can have two possible values:
|
|
382
384
|
* @returns The method returns an array of nodes (`N[]`).
|
|
383
385
|
*/
|
|
384
|
-
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
386
|
+
getNodes(identifier, callback = this._defaultOneParamCallback, onlyOne = false, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
385
387
|
beginRoot = this.ensureNotKey(beginRoot);
|
|
386
388
|
if (!beginRoot)
|
|
387
389
|
return [];
|
|
@@ -462,7 +464,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
462
464
|
* @returns The function `lesserOrGreaterTraverse` returns an array of values of type
|
|
463
465
|
* `ReturnType<C>`, which is the return type of the callback function passed as an argument.
|
|
464
466
|
*/
|
|
465
|
-
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
467
|
+
lesserOrGreaterTraverse(callback = this._defaultOneParamCallback, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.options.iterationType) {
|
|
466
468
|
targetNode = this.ensureNotKey(targetNode);
|
|
467
469
|
const ans = [];
|
|
468
470
|
if (!targetNode)
|
|
@@ -526,7 +528,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
526
528
|
* values:
|
|
527
529
|
* @returns The function `perfectlyBalance` returns a boolean value.
|
|
528
530
|
*/
|
|
529
|
-
perfectlyBalance(iterationType = this.iterationType) {
|
|
531
|
+
perfectlyBalance(iterationType = this.options.iterationType) {
|
|
530
532
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
531
533
|
this.clear();
|
|
532
534
|
if (sorted.length < 1)
|
|
@@ -576,7 +578,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
576
578
|
* to check if the AVL tree is balanced. It can have two possible values:
|
|
577
579
|
* @returns a boolean value.
|
|
578
580
|
*/
|
|
579
|
-
isAVLBalanced(iterationType = this.iterationType) {
|
|
581
|
+
isAVLBalanced(iterationType = this.options.iterationType) {
|
|
580
582
|
var _a, _b;
|
|
581
583
|
if (!this.root)
|
|
582
584
|
return true;
|
|
@@ -637,7 +639,7 @@ class BST extends binary_tree_1.BinaryTree {
|
|
|
637
639
|
* than), CP.lt (less than), or CP.eq (equal).
|
|
638
640
|
*/
|
|
639
641
|
_compare(a, b) {
|
|
640
|
-
const compared = this.
|
|
642
|
+
const compared = this.options.comparator(a, b);
|
|
641
643
|
if (compared > 0)
|
|
642
644
|
return types_1.CP.gt;
|
|
643
645
|
else if (compared < 0)
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
|
-
import { BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNodeNested } from '../../types';
|
|
8
|
+
import { BiTreeDeleteResult, BTNCallback, BTNKey, IterationType, RBTNColor, RBTreeOptions, RedBlackTreeNested, RedBlackTreeNodeNested } from '../../types';
|
|
9
9
|
import { BST, BSTNode } from './bst';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
@@ -15,12 +15,13 @@ export declare class RedBlackTreeNode<V = any, N extends RedBlackTreeNode<V, N>
|
|
|
15
15
|
/**
|
|
16
16
|
* 1. Each node is either red or black.
|
|
17
17
|
* 2. The root node is always black.
|
|
18
|
-
* 3. Leaf nodes are typically
|
|
18
|
+
* 3. Leaf nodes are typically Sentinel nodes and are considered black.
|
|
19
19
|
* 4. Red nodes must have black children.
|
|
20
20
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
21
21
|
*/
|
|
22
|
-
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
|
|
23
|
-
|
|
22
|
+
export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = RedBlackTreeNode<V, RedBlackTreeNodeNested<V>>, TREE extends RedBlackTree<V, N, TREE> = RedBlackTree<V, N, RedBlackTreeNested<V, N>>> extends BST<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
23
|
+
Sentinel: N;
|
|
24
|
+
options: RBTreeOptions;
|
|
24
25
|
/**
|
|
25
26
|
* The constructor function initializes a Red-Black Tree with an optional set of options.
|
|
26
27
|
* @param {RBTreeOptions} [options] - The `options` parameter is an optional object that can be
|
|
@@ -31,6 +32,8 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
31
32
|
get root(): N;
|
|
32
33
|
protected _size: number;
|
|
33
34
|
get size(): number;
|
|
35
|
+
createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
|
|
36
|
+
createTree(options?: RBTreeOptions): TREE;
|
|
34
37
|
/**
|
|
35
38
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
36
39
|
* Space Complexity: O(1)
|
|
@@ -47,7 +50,6 @@ export declare class RedBlackTree<V = any, N extends RedBlackTreeNode<V, N> = Re
|
|
|
47
50
|
* @returns The method returns either a node (`N`) or `undefined`.
|
|
48
51
|
*/
|
|
49
52
|
add(keyOrNode: BTNKey | N | null | undefined, value?: V): N | undefined;
|
|
50
|
-
createNode(key: BTNKey, value?: V, color?: RBTNColor): N;
|
|
51
53
|
/**
|
|
52
54
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
53
55
|
* Space Complexity: O(1)
|
|
@@ -21,7 +21,7 @@ exports.RedBlackTreeNode = RedBlackTreeNode;
|
|
|
21
21
|
/**
|
|
22
22
|
* 1. Each node is either red or black.
|
|
23
23
|
* 2. The root node is always black.
|
|
24
|
-
* 3. Leaf nodes are typically
|
|
24
|
+
* 3. Leaf nodes are typically Sentinel nodes and are considered black.
|
|
25
25
|
* 4. Red nodes must have black children.
|
|
26
26
|
* 5. Black balance: Every path from any node to each of its leaf nodes contains the same number of black nodes.
|
|
27
27
|
*/
|
|
@@ -33,9 +33,15 @@ class RedBlackTree extends bst_1.BST {
|
|
|
33
33
|
*/
|
|
34
34
|
constructor(options) {
|
|
35
35
|
super(options);
|
|
36
|
-
this.
|
|
36
|
+
this.Sentinel = new RedBlackTreeNode(NaN);
|
|
37
37
|
this._size = 0;
|
|
38
|
-
|
|
38
|
+
if (options) {
|
|
39
|
+
this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b }, options);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
43
|
+
}
|
|
44
|
+
this._root = this.Sentinel;
|
|
39
45
|
}
|
|
40
46
|
get root() {
|
|
41
47
|
return this._root;
|
|
@@ -43,6 +49,12 @@ class RedBlackTree extends bst_1.BST {
|
|
|
43
49
|
get size() {
|
|
44
50
|
return this._size;
|
|
45
51
|
}
|
|
52
|
+
createNode(key, value, color = types_1.RBTNColor.BLACK) {
|
|
53
|
+
return new RedBlackTreeNode(key, value, color);
|
|
54
|
+
}
|
|
55
|
+
createTree(options) {
|
|
56
|
+
return new RedBlackTree(Object.assign(Object.assign({}, this.options), options));
|
|
57
|
+
}
|
|
46
58
|
/**
|
|
47
59
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
48
60
|
* Space Complexity: O(1)
|
|
@@ -75,11 +87,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
75
87
|
else {
|
|
76
88
|
return;
|
|
77
89
|
}
|
|
78
|
-
node.left = this.
|
|
79
|
-
node.right = this.
|
|
90
|
+
node.left = this.Sentinel;
|
|
91
|
+
node.right = this.Sentinel;
|
|
80
92
|
let y = undefined;
|
|
81
93
|
let x = this.root;
|
|
82
|
-
while (x !== this.
|
|
94
|
+
while (x !== this.Sentinel) {
|
|
83
95
|
y = x;
|
|
84
96
|
if (x) {
|
|
85
97
|
if (node.key < x.key) {
|
|
@@ -115,9 +127,6 @@ class RedBlackTree extends bst_1.BST {
|
|
|
115
127
|
this._fixInsert(node);
|
|
116
128
|
this._size++;
|
|
117
129
|
}
|
|
118
|
-
createNode(key, value, color = types_1.RBTNColor.BLACK) {
|
|
119
|
-
return new RedBlackTreeNode(key, value, color);
|
|
120
|
-
}
|
|
121
130
|
/**
|
|
122
131
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
123
132
|
* Space Complexity: O(1)
|
|
@@ -142,9 +151,9 @@ class RedBlackTree extends bst_1.BST {
|
|
|
142
151
|
if (identifier === null)
|
|
143
152
|
return ans;
|
|
144
153
|
const helper = (node) => {
|
|
145
|
-
let z = this.
|
|
154
|
+
let z = this.Sentinel;
|
|
146
155
|
let x, y;
|
|
147
|
-
while (node !== this.
|
|
156
|
+
while (node !== this.Sentinel) {
|
|
148
157
|
if (node && callback(node) === identifier) {
|
|
149
158
|
z = node;
|
|
150
159
|
}
|
|
@@ -155,17 +164,17 @@ class RedBlackTree extends bst_1.BST {
|
|
|
155
164
|
node = node === null || node === void 0 ? void 0 : node.left;
|
|
156
165
|
}
|
|
157
166
|
}
|
|
158
|
-
if (z === this.
|
|
167
|
+
if (z === this.Sentinel) {
|
|
159
168
|
this._size--;
|
|
160
169
|
return;
|
|
161
170
|
}
|
|
162
171
|
y = z;
|
|
163
172
|
let yOriginalColor = y.color;
|
|
164
|
-
if (z.left === this.
|
|
173
|
+
if (z.left === this.Sentinel) {
|
|
165
174
|
x = z.right;
|
|
166
175
|
this._rbTransplant(z, z.right);
|
|
167
176
|
}
|
|
168
|
-
else if (z.right === this.
|
|
177
|
+
else if (z.right === this.Sentinel) {
|
|
169
178
|
x = z.left;
|
|
170
179
|
this._rbTransplant(z, z.left);
|
|
171
180
|
}
|
|
@@ -196,7 +205,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
196
205
|
return ans;
|
|
197
206
|
}
|
|
198
207
|
isRealNode(node) {
|
|
199
|
-
return node !== this.
|
|
208
|
+
return node !== this.Sentinel && node !== undefined;
|
|
200
209
|
}
|
|
201
210
|
/**
|
|
202
211
|
* Time Complexity: O(log n) on average (where n is the number of nodes in the tree)
|
|
@@ -223,7 +232,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
223
232
|
* `getNodes` method, which is called within the `getNode` method.
|
|
224
233
|
* @returns a value of type `N`, `null`, or `undefined`.
|
|
225
234
|
*/
|
|
226
|
-
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.iterationType) {
|
|
235
|
+
getNode(identifier, callback = this._defaultOneParamCallback, beginRoot = this.root, iterationType = this.options.iterationType) {
|
|
227
236
|
var _a;
|
|
228
237
|
if (identifier instanceof binary_tree_1.BinaryTreeNode)
|
|
229
238
|
callback = (node => node);
|
|
@@ -244,11 +253,11 @@ class RedBlackTree extends bst_1.BST {
|
|
|
244
253
|
*/
|
|
245
254
|
getSuccessor(x) {
|
|
246
255
|
var _a;
|
|
247
|
-
if (x.right !== this.
|
|
256
|
+
if (x.right !== this.Sentinel) {
|
|
248
257
|
return (_a = this.getLeftMost(x.right)) !== null && _a !== void 0 ? _a : undefined;
|
|
249
258
|
}
|
|
250
259
|
let y = x.parent;
|
|
251
|
-
while (y !== this.
|
|
260
|
+
while (y !== this.Sentinel && y !== undefined && x === y.right) {
|
|
252
261
|
x = y;
|
|
253
262
|
y = y.parent;
|
|
254
263
|
}
|
|
@@ -268,18 +277,18 @@ class RedBlackTree extends bst_1.BST {
|
|
|
268
277
|
* @returns the predecessor of the given RedBlackTreeNode 'x'.
|
|
269
278
|
*/
|
|
270
279
|
getPredecessor(x) {
|
|
271
|
-
if (x.left !== this.
|
|
280
|
+
if (x.left !== this.Sentinel) {
|
|
272
281
|
return this.getRightMost(x.left);
|
|
273
282
|
}
|
|
274
283
|
let y = x.parent;
|
|
275
|
-
while (y !== this.
|
|
284
|
+
while (y !== this.Sentinel && x === y.left) {
|
|
276
285
|
x = y;
|
|
277
286
|
y = y.parent;
|
|
278
287
|
}
|
|
279
288
|
return y;
|
|
280
289
|
}
|
|
281
290
|
clear() {
|
|
282
|
-
this._root = this.
|
|
291
|
+
this._root = this.Sentinel;
|
|
283
292
|
this._size = 0;
|
|
284
293
|
}
|
|
285
294
|
_setRoot(v) {
|
|
@@ -303,7 +312,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
303
312
|
if (x.right) {
|
|
304
313
|
const y = x.right;
|
|
305
314
|
x.right = y.left;
|
|
306
|
-
if (y.left !== this.
|
|
315
|
+
if (y.left !== this.Sentinel) {
|
|
307
316
|
if (y.left)
|
|
308
317
|
y.left.parent = x;
|
|
309
318
|
}
|
|
@@ -337,7 +346,7 @@ class RedBlackTree extends bst_1.BST {
|
|
|
337
346
|
if (x.left) {
|
|
338
347
|
const y = x.left;
|
|
339
348
|
x.left = y.right;
|
|
340
|
-
if (y.right !== this.
|
|
349
|
+
if (y.right !== this.Sentinel) {
|
|
341
350
|
if (y.right)
|
|
342
351
|
y.right.parent = x;
|
|
343
352
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BTNKey, TreeMultimapNodeNested, TreeMultimapOptions } from '../../types';
|
|
9
|
-
import { BiTreeDeleteResult, BTNCallback, IterationType } from '../../types';
|
|
9
|
+
import { BiTreeDeleteResult, BTNCallback, IterationType, TreeMultimapNested } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNodeNested<V>> extends AVLTreeNode<V, N> {
|
|
@@ -26,7 +26,8 @@ export declare class TreeMultimapNode<V = any, N extends TreeMultimapNode<V, N>
|
|
|
26
26
|
/**
|
|
27
27
|
* The only distinction between a TreeMultimap and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
|
|
28
28
|
*/
|
|
29
|
-
export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>> extends AVLTree<V, N> implements IBinaryTree<V, N> {
|
|
29
|
+
export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = TreeMultimapNode<V, TreeMultimapNodeNested<V>>, TREE extends TreeMultimap<V, N, TREE> = TreeMultimap<V, N, TreeMultimapNested<V, N>>> extends AVLTree<V, N, TREE> implements IBinaryTree<V, N, TREE> {
|
|
30
|
+
options: TreeMultimapOptions;
|
|
30
31
|
/**
|
|
31
32
|
* The constructor function for a TreeMultimap class in TypeScript, which extends another class and sets an option to
|
|
32
33
|
* merge duplicated values.
|
|
@@ -46,6 +47,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
46
47
|
* @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
|
|
47
48
|
*/
|
|
48
49
|
createNode(key: BTNKey, value?: V, count?: number): N;
|
|
50
|
+
createTree(options?: TreeMultimapOptions): TREE;
|
|
49
51
|
/**
|
|
50
52
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
51
53
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -98,7 +100,7 @@ export declare class TreeMultimap<V = any, N extends TreeMultimapNode<V, N> = Tr
|
|
|
98
100
|
* values:
|
|
99
101
|
* @returns a boolean value.
|
|
100
102
|
*/
|
|
101
|
-
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
103
|
+
perfectlyBalance(iterationType?: IterationType | undefined): boolean;
|
|
102
104
|
/**
|
|
103
105
|
* Time Complexity: O(n log n) - logarithmic time for each insertion, where "n" is the number of nodes in the tree. This is because the method calls the add method for each node.
|
|
104
106
|
* Space Complexity: O(n) - linear space, as it creates an array to store the sorted nodes.
|
|
@@ -30,9 +30,15 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
30
30
|
* @param {TreeMultimapOptions} [options] - An optional object that contains additional configuration options for the
|
|
31
31
|
* TreeMultimap.
|
|
32
32
|
*/
|
|
33
|
-
constructor(options) {
|
|
33
|
+
constructor(options = { iterationType: types_1.IterationType.ITERATIVE }) {
|
|
34
34
|
super(options);
|
|
35
35
|
this._count = 0;
|
|
36
|
+
if (options) {
|
|
37
|
+
this.options = Object.assign({ iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b }, options);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this.options = { iterationType: types_1.IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
41
|
+
}
|
|
36
42
|
}
|
|
37
43
|
get count() {
|
|
38
44
|
return this._count;
|
|
@@ -49,6 +55,9 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
49
55
|
createNode(key, value, count) {
|
|
50
56
|
return new TreeMultimapNode(key, value, count);
|
|
51
57
|
}
|
|
58
|
+
createTree(options) {
|
|
59
|
+
return new TreeMultimap(Object.assign(Object.assign({}, this.options), options));
|
|
60
|
+
}
|
|
52
61
|
/**
|
|
53
62
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (AVLTree) has logarithmic time complexity.
|
|
54
63
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|
|
@@ -194,7 +203,7 @@ class TreeMultimap extends avl_tree_1.AVLTree {
|
|
|
194
203
|
* values:
|
|
195
204
|
* @returns a boolean value.
|
|
196
205
|
*/
|
|
197
|
-
perfectlyBalance(iterationType = this.iterationType) {
|
|
206
|
+
perfectlyBalance(iterationType = this.options.iterationType) {
|
|
198
207
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
199
208
|
if (sorted.length < 1)
|
|
200
209
|
return false;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { BinaryTreeNode } from '../data-structures';
|
|
2
|
-
import { BinaryTreeNodeNested, BiTreeDeleteResult, BTNCallback, BTNKey } from '../types';
|
|
3
|
-
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
|
|
1
|
+
import { BinaryTree, BinaryTreeNode } from '../data-structures';
|
|
2
|
+
import { BinaryTreeNested, BinaryTreeNodeNested, BiTreeDeleteResult, BTNCallback, BTNKey } from '../types';
|
|
3
|
+
export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>, TREE extends BinaryTree<V, N, TREE> = BinaryTreeNested<V, N>> {
|
|
4
4
|
createNode(key: BTNKey, value?: N['value']): N;
|
|
5
5
|
add(keyOrNode: BTNKey | N | null, value?: N['value']): N | null | undefined;
|
|
6
6
|
delete<C extends BTNCallback<N>>(identifier: ReturnType<C> | null, callback: C): BiTreeDeleteResult<N>[];
|
package/dist/types/common.d.ts
CHANGED
|
@@ -6,10 +6,6 @@ export declare enum CP {
|
|
|
6
6
|
eq = "eq",
|
|
7
7
|
gt = "gt"
|
|
8
8
|
}
|
|
9
|
-
export declare const enum IterateDirection {
|
|
10
|
-
DEFAULT = 0,
|
|
11
|
-
REVERSE = 1
|
|
12
|
-
}
|
|
13
9
|
export interface IterableWithSize<T> extends Iterable<T> {
|
|
14
10
|
size: number | ((...args: any[]) => number);
|
|
15
11
|
}
|
|
@@ -17,3 +13,8 @@ export interface IterableWithLength<T> extends Iterable<T> {
|
|
|
17
13
|
length: number | ((...args: any[]) => number);
|
|
18
14
|
}
|
|
19
15
|
export type IterableWithSizeOrLength<T> = IterableWithSize<T> | IterableWithLength<T>;
|
|
16
|
+
export type BinaryTreePrintOptions = {
|
|
17
|
+
isShowUndefined?: boolean;
|
|
18
|
+
isShowNull?: boolean;
|
|
19
|
+
isShowRedBlackNIL?: boolean;
|
|
20
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { AVLTreeNode } from '../../../data-structures';
|
|
1
|
+
import { AVLTree, AVLTreeNode } from '../../../data-structures';
|
|
2
2
|
import { BSTOptions } from './bst';
|
|
3
3
|
export type AVLTreeNodeNested<T> = AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, AVLTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
|
+
export type AVLTreeNested<T, N extends AVLTreeNode<T, N>> = AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, AVLTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
5
|
export type AVLTreeOptions = BSTOptions & {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BinaryTreeNode } from '../../../data-structures';
|
|
1
|
+
import { BinaryTree, BinaryTreeNode } from '../../../data-structures';
|
|
2
2
|
/**
|
|
3
3
|
* Enum representing different loop types.
|
|
4
4
|
*
|
|
@@ -24,6 +24,8 @@ export type BiTreeDeleteResult<N> = {
|
|
|
24
24
|
needBalanced: N | null | undefined;
|
|
25
25
|
};
|
|
26
26
|
export type BinaryTreeNodeNested<T> = BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, BinaryTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
27
|
+
export type BinaryTreeNested<T, N extends BinaryTreeNode<T, N>> = BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, BinaryTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
27
28
|
export type BinaryTreeOptions = {
|
|
28
29
|
iterationType?: IterationType;
|
|
29
30
|
};
|
|
31
|
+
export type NodeDisplayLayout = [string[], number, number, number];
|
|
@@ -22,9 +22,3 @@ var FamilyPosition;
|
|
|
22
22
|
FamilyPosition["ISOLATED"] = "ISOLATED";
|
|
23
23
|
FamilyPosition["MAL_NODE"] = "MAL_NODE";
|
|
24
24
|
})(FamilyPosition = exports.FamilyPosition || (exports.FamilyPosition = {}));
|
|
25
|
-
//
|
|
26
|
-
// export type BTNIdentifierOrNU<N> = BTNKey | N | null | undefined;
|
|
27
|
-
//
|
|
28
|
-
// export type BTNIdentifierOrU<N> = BTNKey | N | undefined;
|
|
29
|
-
//
|
|
30
|
-
// export type BTNOrNU<N> = N | null | undefined;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { BSTNode } from '../../../data-structures';
|
|
1
|
+
import { BST, BSTNode } from '../../../data-structures';
|
|
2
2
|
import type { BinaryTreeOptions, BTNKey } from './binary-tree';
|
|
3
3
|
export type BSTComparator = (a: BTNKey, b: BTNKey) => number;
|
|
4
4
|
export type BSTNodeNested<T> = BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, BSTNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
|
+
export type BSTNested<T, N extends BSTNode<T, N>> = BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, BST<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
5
6
|
export type BSTOptions = BinaryTreeOptions & {
|
|
6
7
|
comparator?: BSTComparator;
|
|
7
8
|
};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { RedBlackTreeNode } from '../../../data-structures';
|
|
1
|
+
import { RedBlackTree, RedBlackTreeNode } from '../../../data-structures';
|
|
2
2
|
import { BSTOptions } from "./bst";
|
|
3
3
|
export declare enum RBTNColor {
|
|
4
4
|
RED = 1,
|
|
5
5
|
BLACK = 0
|
|
6
6
|
}
|
|
7
7
|
export type RedBlackTreeNodeNested<T> = RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, RedBlackTreeNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
8
|
+
export type RedBlackTreeNested<T, N extends RedBlackTreeNode<T, N>> = RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, RedBlackTree<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
8
9
|
export type RBTreeOptions = BSTOptions & {};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { TreeMultimapNode } from '../../../data-structures';
|
|
1
|
+
import { TreeMultimap, TreeMultimapNode } from '../../../data-structures';
|
|
2
2
|
import { AVLTreeOptions } from './avl-tree';
|
|
3
3
|
export type TreeMultimapNodeNested<T> = TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, TreeMultimapNode<T, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
|
+
export type TreeMultimapNested<T, N extends TreeMultimapNode<T, N>> = TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, TreeMultimap<T, N, any>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
4
5
|
export type TreeMultimapOptions = Omit<AVLTreeOptions, 'isMergeDuplicatedNodeByKey'> & {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "linked-list-typed",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.47.1",
|
|
4
4
|
"description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"typescript": "^4.9.5"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"data-structure-typed": "^1.
|
|
69
|
+
"data-structure-typed": "^1.47.1"
|
|
70
70
|
}
|
|
71
71
|
}
|
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import { BST, BSTNode } from './bst';
|
|
9
|
-
import type { AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
|
|
10
|
-
import { BTNCallback } from '../../types';
|
|
9
|
+
import type { AVLTreeNested, AVLTreeNodeNested, AVLTreeOptions, BiTreeDeleteResult, BTNKey } from '../../types';
|
|
10
|
+
import { BTNCallback, IterationType } from '../../types';
|
|
11
11
|
import { IBinaryTree } from '../../interfaces';
|
|
12
12
|
|
|
13
13
|
export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
|
|
@@ -19,9 +19,12 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
|
|
23
|
-
extends BST<V, N>
|
|
24
|
-
implements IBinaryTree<V, N> {
|
|
22
|
+
export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>, TREE extends AVLTree<V, N, TREE> = AVLTree<V, N, AVLTreeNested<V, N>>>
|
|
23
|
+
extends BST<V, N, TREE>
|
|
24
|
+
implements IBinaryTree<V, N, TREE> {
|
|
25
|
+
|
|
26
|
+
override options: AVLTreeOptions;
|
|
27
|
+
|
|
25
28
|
/**
|
|
26
29
|
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
27
30
|
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
@@ -30,6 +33,11 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
30
33
|
*/
|
|
31
34
|
constructor(options?: AVLTreeOptions) {
|
|
32
35
|
super(options);
|
|
36
|
+
if (options) {
|
|
37
|
+
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b, ...options }
|
|
38
|
+
} else {
|
|
39
|
+
this.options = { iterationType: IterationType.ITERATIVE, comparator: (a, b) => a - b };
|
|
40
|
+
}
|
|
33
41
|
}
|
|
34
42
|
|
|
35
43
|
/**
|
|
@@ -45,6 +53,10 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
|
|
|
45
53
|
return new AVLTreeNode<V, N>(key, value) as N;
|
|
46
54
|
}
|
|
47
55
|
|
|
56
|
+
override createTree(options?: AVLTreeOptions): TREE {
|
|
57
|
+
return new AVLTree<V, N, TREE>({ ...this.options, ...options }) as TREE;
|
|
58
|
+
}
|
|
59
|
+
|
|
48
60
|
/**
|
|
49
61
|
* Time Complexity: O(log n) - logarithmic time, where "n" is the number of nodes in the tree. The add method of the superclass (BST) has logarithmic time complexity.
|
|
50
62
|
* Space Complexity: O(1) - constant space, as it doesn't use additional data structures that scale with input size.
|