trie-typed 2.1.2 → 2.2.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/cjs/index.cjs +30 -28
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs-legacy/index.cjs +795 -0
- package/dist/cjs-legacy/index.cjs.map +1 -0
- package/dist/esm/index.mjs +30 -28
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm-legacy/index.mjs +790 -0
- package/dist/esm-legacy/index.mjs.map +1 -0
- package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +65 -3
- package/dist/types/data-structures/binary-tree/avl-tree.d.ts +58 -4
- package/dist/types/data-structures/binary-tree/bst.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +58 -4
- package/dist/types/data-structures/binary-tree/tree-counter.d.ts +57 -3
- package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +65 -3
- package/package.json +20 -2
- package/src/data-structures/binary-tree/avl-tree-counter.ts +102 -11
- package/src/data-structures/binary-tree/avl-tree-multi-map.ts +115 -11
- package/src/data-structures/binary-tree/avl-tree.ts +105 -14
- package/src/data-structures/binary-tree/bst.ts +102 -11
- package/src/data-structures/binary-tree/red-black-tree.ts +108 -18
- package/src/data-structures/binary-tree/tree-counter.ts +101 -10
- package/src/data-structures/binary-tree/tree-multi-map.ts +122 -11
- package/src/data-structures/graph/abstract-graph.ts +5 -5
- package/src/data-structures/graph/directed-graph.ts +5 -5
- package/src/data-structures/graph/undirected-graph.ts +5 -5
- package/tsup.node.config.js +40 -6
|
@@ -6,14 +6,16 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { BST
|
|
9
|
+
import { BST } from './bst';
|
|
10
10
|
import type {
|
|
11
11
|
AVLTreeOptions,
|
|
12
12
|
BinaryTreeDeleteResult,
|
|
13
13
|
BinaryTreeOptions,
|
|
14
14
|
BSTNOptKeyOrNode,
|
|
15
15
|
EntryCallback,
|
|
16
|
-
|
|
16
|
+
FamilyPosition,
|
|
17
|
+
IterationType,
|
|
18
|
+
RBTNColor
|
|
17
19
|
} from '../../types';
|
|
18
20
|
import { BSTOptions } from '../../types';
|
|
19
21
|
import { IBinaryTree } from '../../interfaces';
|
|
@@ -25,8 +27,10 @@ import { IBinaryTree } from '../../interfaces';
|
|
|
25
27
|
* @template K - The type of the key.
|
|
26
28
|
* @template V - The type of the value.
|
|
27
29
|
*/
|
|
28
|
-
export class AVLTreeNode<K = any, V = any>
|
|
29
|
-
|
|
30
|
+
export class AVLTreeNode<K = any, V = any> {
|
|
31
|
+
key: K;
|
|
32
|
+
value?: V;
|
|
33
|
+
parent?: AVLTreeNode<K, V> = undefined;
|
|
30
34
|
|
|
31
35
|
/**
|
|
32
36
|
* Creates an instance of AVLTreeNode.
|
|
@@ -36,10 +40,11 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
36
40
|
* @param [value] - The value associated with the key.
|
|
37
41
|
*/
|
|
38
42
|
constructor(key: K, value?: V) {
|
|
39
|
-
|
|
43
|
+
this.key = key;
|
|
44
|
+
this.value = value;
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
|
|
47
|
+
_left?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
43
48
|
|
|
44
49
|
/**
|
|
45
50
|
* Gets the left child of the node.
|
|
@@ -47,7 +52,7 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
47
52
|
*
|
|
48
53
|
* @returns The left child.
|
|
49
54
|
*/
|
|
50
|
-
|
|
55
|
+
get left(): AVLTreeNode<K, V> | null | undefined {
|
|
51
56
|
return this._left;
|
|
52
57
|
}
|
|
53
58
|
|
|
@@ -57,14 +62,14 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
57
62
|
*
|
|
58
63
|
* @param v - The node to set as the left child.
|
|
59
64
|
*/
|
|
60
|
-
|
|
65
|
+
set left(v: AVLTreeNode<K, V> | null | undefined) {
|
|
61
66
|
if (v) {
|
|
62
67
|
v.parent = this;
|
|
63
68
|
}
|
|
64
69
|
this._left = v;
|
|
65
70
|
}
|
|
66
71
|
|
|
67
|
-
|
|
72
|
+
_right?: AVLTreeNode<K, V> | null | undefined = undefined;
|
|
68
73
|
|
|
69
74
|
/**
|
|
70
75
|
* Gets the right child of the node.
|
|
@@ -72,7 +77,7 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
72
77
|
*
|
|
73
78
|
* @returns The right child.
|
|
74
79
|
*/
|
|
75
|
-
|
|
80
|
+
get right(): AVLTreeNode<K, V> | null | undefined {
|
|
76
81
|
return this._right;
|
|
77
82
|
}
|
|
78
83
|
|
|
@@ -82,12 +87,98 @@ export class AVLTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
82
87
|
*
|
|
83
88
|
* @param v - The node to set as the right child.
|
|
84
89
|
*/
|
|
85
|
-
|
|
90
|
+
set right(v: AVLTreeNode<K, V> | null | undefined) {
|
|
86
91
|
if (v) {
|
|
87
92
|
v.parent = this;
|
|
88
93
|
}
|
|
89
94
|
this._right = v;
|
|
90
95
|
}
|
|
96
|
+
|
|
97
|
+
_height: number = 0;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
101
|
+
* @remarks Time O(1), Space O(1)
|
|
102
|
+
*
|
|
103
|
+
* @returns The height.
|
|
104
|
+
*/
|
|
105
|
+
get height(): number {
|
|
106
|
+
return this._height;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Sets the height of the node.
|
|
111
|
+
* @remarks Time O(1), Space O(1)
|
|
112
|
+
*
|
|
113
|
+
* @param value - The new height.
|
|
114
|
+
*/
|
|
115
|
+
set height(value: number) {
|
|
116
|
+
this._height = value;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
_color: RBTNColor = 'BLACK';
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
123
|
+
* @remarks Time O(1), Space O(1)
|
|
124
|
+
*
|
|
125
|
+
* @returns The node's color.
|
|
126
|
+
*/
|
|
127
|
+
get color(): RBTNColor {
|
|
128
|
+
return this._color;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Sets the color of the node.
|
|
133
|
+
* @remarks Time O(1), Space O(1)
|
|
134
|
+
*
|
|
135
|
+
* @param value - The new color.
|
|
136
|
+
*/
|
|
137
|
+
set color(value: RBTNColor) {
|
|
138
|
+
this._color = value;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
_count: number = 1;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
145
|
+
* @remarks Time O(1), Space O(1)
|
|
146
|
+
*
|
|
147
|
+
* @returns The subtree node count.
|
|
148
|
+
*/
|
|
149
|
+
get count(): number {
|
|
150
|
+
return this._count;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Sets the count of nodes in the subtree.
|
|
155
|
+
* @remarks Time O(1), Space O(1)
|
|
156
|
+
*
|
|
157
|
+
* @param value - The new count.
|
|
158
|
+
*/
|
|
159
|
+
set count(value: number) {
|
|
160
|
+
this._count = value;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Gets the position of the node relative to its parent.
|
|
165
|
+
* @remarks Time O(1), Space O(1)
|
|
166
|
+
*
|
|
167
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
168
|
+
*/
|
|
169
|
+
get familyPosition(): FamilyPosition {
|
|
170
|
+
if (!this.parent) {
|
|
171
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (this.parent.left === this) {
|
|
175
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
176
|
+
} else if (this.parent.right === this) {
|
|
177
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
return 'MAL_NODE';
|
|
181
|
+
}
|
|
91
182
|
}
|
|
92
183
|
|
|
93
184
|
/**
|
|
@@ -327,7 +418,7 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
327
418
|
*/
|
|
328
419
|
protected override _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BSTOptions<TK, TV, TR>>): this {
|
|
329
420
|
const Ctor = this.constructor as unknown as new (
|
|
330
|
-
iter?: Iterable<TK |
|
|
421
|
+
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
331
422
|
opts?: BSTOptions<TK, TV, TR>
|
|
332
423
|
) => this;
|
|
333
424
|
return new Ctor([], { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) }) as unknown as this;
|
|
@@ -343,11 +434,11 @@ export class AVLTree<K = any, V = any, R = any> extends BST<K, V, R> implements
|
|
|
343
434
|
* @returns A new AVLTree.
|
|
344
435
|
*/
|
|
345
436
|
protected override _createLike<TK = K, TV = V, TR = R>(
|
|
346
|
-
iter: Iterable<TK |
|
|
437
|
+
iter: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR> = [],
|
|
347
438
|
options?: Partial<BSTOptions<TK, TV, TR>>
|
|
348
439
|
): AVLTree<TK, TV, TR> {
|
|
349
440
|
const Ctor = this.constructor as unknown as new (
|
|
350
|
-
iter?: Iterable<TK |
|
|
441
|
+
iter?: Iterable<TK | AVLTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>,
|
|
351
442
|
opts?: BSTOptions<TK, TV, TR>
|
|
352
443
|
) => AVLTree<TK, TV, TR>;
|
|
353
444
|
return new Ctor(iter, { ...this._snapshotOptions<TK, TV, TR>(), ...(options ?? {}) });
|
|
@@ -16,12 +16,14 @@ import type {
|
|
|
16
16
|
CP,
|
|
17
17
|
DFSOrderPattern,
|
|
18
18
|
EntryCallback,
|
|
19
|
+
FamilyPosition,
|
|
19
20
|
IterationType,
|
|
20
21
|
NodeCallback,
|
|
21
22
|
NodePredicate,
|
|
22
|
-
OptNode
|
|
23
|
+
OptNode,
|
|
24
|
+
RBTNColor
|
|
23
25
|
} from '../../types';
|
|
24
|
-
import { BinaryTree
|
|
26
|
+
import { BinaryTree } from './binary-tree';
|
|
25
27
|
import { IBinaryTree } from '../../interfaces';
|
|
26
28
|
import { Queue } from '../queue';
|
|
27
29
|
import { isComparable } from '../../utils';
|
|
@@ -33,8 +35,10 @@ import { Range } from '../../common';
|
|
|
33
35
|
* @template K - The type of the key.
|
|
34
36
|
* @template V - The type of the value.
|
|
35
37
|
*/
|
|
36
|
-
export class BSTNode<K = any, V = any>
|
|
37
|
-
|
|
38
|
+
export class BSTNode<K = any, V = any> {
|
|
39
|
+
key: K;
|
|
40
|
+
value?: V;
|
|
41
|
+
parent?: BSTNode<K, V> = undefined;
|
|
38
42
|
|
|
39
43
|
/**
|
|
40
44
|
* Creates an instance of BSTNode.
|
|
@@ -44,10 +48,11 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
44
48
|
* @param [value] - The value associated with the key.
|
|
45
49
|
*/
|
|
46
50
|
constructor(key: K, value?: V) {
|
|
47
|
-
|
|
51
|
+
this.key = key;
|
|
52
|
+
this.value = value;
|
|
48
53
|
}
|
|
49
54
|
|
|
50
|
-
|
|
55
|
+
_left?: BSTNode<K, V> | null | undefined = undefined;
|
|
51
56
|
|
|
52
57
|
/**
|
|
53
58
|
* Gets the left child of the node.
|
|
@@ -55,7 +60,7 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
55
60
|
*
|
|
56
61
|
* @returns The left child.
|
|
57
62
|
*/
|
|
58
|
-
|
|
63
|
+
get left(): BSTNode<K, V> | null | undefined {
|
|
59
64
|
return this._left;
|
|
60
65
|
}
|
|
61
66
|
|
|
@@ -65,12 +70,12 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
65
70
|
*
|
|
66
71
|
* @param v - The node to set as the left child.
|
|
67
72
|
*/
|
|
68
|
-
|
|
73
|
+
set left(v: BSTNode<K, V> | null | undefined) {
|
|
69
74
|
if (v) v.parent = this;
|
|
70
75
|
this._left = v;
|
|
71
76
|
}
|
|
72
77
|
|
|
73
|
-
|
|
78
|
+
_right?: BSTNode<K, V> | null | undefined = undefined;
|
|
74
79
|
|
|
75
80
|
/**
|
|
76
81
|
* Gets the right child of the node.
|
|
@@ -78,7 +83,7 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
78
83
|
*
|
|
79
84
|
* @returns The right child.
|
|
80
85
|
*/
|
|
81
|
-
|
|
86
|
+
get right(): BSTNode<K, V> | null | undefined {
|
|
82
87
|
return this._right;
|
|
83
88
|
}
|
|
84
89
|
|
|
@@ -88,10 +93,96 @@ export class BSTNode<K = any, V = any> extends BinaryTreeNode<K, V> {
|
|
|
88
93
|
*
|
|
89
94
|
* @param v - The node to set as the right child.
|
|
90
95
|
*/
|
|
91
|
-
|
|
96
|
+
set right(v: BSTNode<K, V> | null | undefined) {
|
|
92
97
|
if (v) v.parent = this;
|
|
93
98
|
this._right = v;
|
|
94
99
|
}
|
|
100
|
+
|
|
101
|
+
_height: number = 0;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
105
|
+
* @remarks Time O(1), Space O(1)
|
|
106
|
+
*
|
|
107
|
+
* @returns The height.
|
|
108
|
+
*/
|
|
109
|
+
get height(): number {
|
|
110
|
+
return this._height;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Sets the height of the node.
|
|
115
|
+
* @remarks Time O(1), Space O(1)
|
|
116
|
+
*
|
|
117
|
+
* @param value - The new height.
|
|
118
|
+
*/
|
|
119
|
+
set height(value: number) {
|
|
120
|
+
this._height = value;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
_color: RBTNColor = 'BLACK';
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
127
|
+
* @remarks Time O(1), Space O(1)
|
|
128
|
+
*
|
|
129
|
+
* @returns The node's color.
|
|
130
|
+
*/
|
|
131
|
+
get color(): RBTNColor {
|
|
132
|
+
return this._color;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Sets the color of the node.
|
|
137
|
+
* @remarks Time O(1), Space O(1)
|
|
138
|
+
*
|
|
139
|
+
* @param value - The new color.
|
|
140
|
+
*/
|
|
141
|
+
set color(value: RBTNColor) {
|
|
142
|
+
this._color = value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
_count: number = 1;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
149
|
+
* @remarks Time O(1), Space O(1)
|
|
150
|
+
*
|
|
151
|
+
* @returns The subtree node count.
|
|
152
|
+
*/
|
|
153
|
+
get count(): number {
|
|
154
|
+
return this._count;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Sets the count of nodes in the subtree.
|
|
159
|
+
* @remarks Time O(1), Space O(1)
|
|
160
|
+
*
|
|
161
|
+
* @param value - The new count.
|
|
162
|
+
*/
|
|
163
|
+
set count(value: number) {
|
|
164
|
+
this._count = value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Gets the position of the node relative to its parent.
|
|
169
|
+
* @remarks Time O(1), Space O(1)
|
|
170
|
+
*
|
|
171
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
172
|
+
*/
|
|
173
|
+
get familyPosition(): FamilyPosition {
|
|
174
|
+
if (!this.parent) {
|
|
175
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (this.parent.left === this) {
|
|
179
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
180
|
+
} else if (this.parent.right === this) {
|
|
181
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return 'MAL_NODE';
|
|
185
|
+
}
|
|
95
186
|
}
|
|
96
187
|
|
|
97
188
|
/**
|
|
@@ -11,15 +11,18 @@ import type {
|
|
|
11
11
|
BinaryTreeOptions,
|
|
12
12
|
CRUD,
|
|
13
13
|
EntryCallback,
|
|
14
|
+
FamilyPosition,
|
|
14
15
|
OptNode,
|
|
15
16
|
RBTNColor,
|
|
16
17
|
RedBlackTreeOptions
|
|
17
18
|
} from '../../types';
|
|
18
|
-
import { BST
|
|
19
|
+
import { BST } from './bst';
|
|
19
20
|
import { IBinaryTree } from '../../interfaces';
|
|
20
21
|
|
|
21
|
-
export class RedBlackTreeNode<K = any, V = any>
|
|
22
|
-
|
|
22
|
+
export class RedBlackTreeNode<K = any, V = any> {
|
|
23
|
+
key: K;
|
|
24
|
+
value?: V;
|
|
25
|
+
parent?: RedBlackTreeNode<K, V> = undefined;
|
|
23
26
|
|
|
24
27
|
/**
|
|
25
28
|
* Create a Red-Black Tree and optionally bulk-insert items.
|
|
@@ -31,11 +34,12 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
31
34
|
*/
|
|
32
35
|
|
|
33
36
|
constructor(key: K, value?: V, color: RBTNColor = 'BLACK') {
|
|
34
|
-
|
|
35
|
-
this.
|
|
37
|
+
this.key = key;
|
|
38
|
+
this.value = value;
|
|
39
|
+
this.color = color;
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
_left?: RedBlackTreeNode<K, V> | null | undefined = undefined;
|
|
39
43
|
|
|
40
44
|
/**
|
|
41
45
|
* Get the left child pointer.
|
|
@@ -43,7 +47,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
43
47
|
* @returns Left child node, or null/undefined.
|
|
44
48
|
*/
|
|
45
49
|
|
|
46
|
-
|
|
50
|
+
get left(): RedBlackTreeNode<K, V> | null | undefined {
|
|
47
51
|
return this._left;
|
|
48
52
|
}
|
|
49
53
|
|
|
@@ -54,14 +58,14 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
54
58
|
* @returns void
|
|
55
59
|
*/
|
|
56
60
|
|
|
57
|
-
|
|
61
|
+
set left(v: RedBlackTreeNode<K, V> | null | undefined) {
|
|
58
62
|
if (v) {
|
|
59
63
|
v.parent = this;
|
|
60
64
|
}
|
|
61
65
|
this._left = v;
|
|
62
66
|
}
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
_right?: RedBlackTreeNode<K, V> | null | undefined = undefined;
|
|
65
69
|
|
|
66
70
|
/**
|
|
67
71
|
* Get the right child pointer.
|
|
@@ -69,7 +73,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
69
73
|
* @returns Right child node, or null/undefined.
|
|
70
74
|
*/
|
|
71
75
|
|
|
72
|
-
|
|
76
|
+
get right(): RedBlackTreeNode<K, V> | null | undefined {
|
|
73
77
|
return this._right;
|
|
74
78
|
}
|
|
75
79
|
|
|
@@ -80,12 +84,98 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
80
84
|
* @returns void
|
|
81
85
|
*/
|
|
82
86
|
|
|
83
|
-
|
|
87
|
+
set right(v: RedBlackTreeNode<K, V> | null | undefined) {
|
|
84
88
|
if (v) {
|
|
85
89
|
v.parent = this;
|
|
86
90
|
}
|
|
87
91
|
this._right = v;
|
|
88
92
|
}
|
|
93
|
+
|
|
94
|
+
_height: number = 0;
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Gets the height of the node (used in self-balancing trees).
|
|
98
|
+
* @remarks Time O(1), Space O(1)
|
|
99
|
+
*
|
|
100
|
+
* @returns The height.
|
|
101
|
+
*/
|
|
102
|
+
get height(): number {
|
|
103
|
+
return this._height;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Sets the height of the node.
|
|
108
|
+
* @remarks Time O(1), Space O(1)
|
|
109
|
+
*
|
|
110
|
+
* @param value - The new height.
|
|
111
|
+
*/
|
|
112
|
+
set height(value: number) {
|
|
113
|
+
this._height = value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
_color: RBTNColor = 'BLACK';
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Gets the color of the node (used in Red-Black trees).
|
|
120
|
+
* @remarks Time O(1), Space O(1)
|
|
121
|
+
*
|
|
122
|
+
* @returns The node's color.
|
|
123
|
+
*/
|
|
124
|
+
get color(): RBTNColor {
|
|
125
|
+
return this._color;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Sets the color of the node.
|
|
130
|
+
* @remarks Time O(1), Space O(1)
|
|
131
|
+
*
|
|
132
|
+
* @param value - The new color.
|
|
133
|
+
*/
|
|
134
|
+
set color(value: RBTNColor) {
|
|
135
|
+
this._color = value;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
_count: number = 1;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
|
|
142
|
+
* @remarks Time O(1), Space O(1)
|
|
143
|
+
*
|
|
144
|
+
* @returns The subtree node count.
|
|
145
|
+
*/
|
|
146
|
+
get count(): number {
|
|
147
|
+
return this._count;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Sets the count of nodes in the subtree.
|
|
152
|
+
* @remarks Time O(1), Space O(1)
|
|
153
|
+
*
|
|
154
|
+
* @param value - The new count.
|
|
155
|
+
*/
|
|
156
|
+
set count(value: number) {
|
|
157
|
+
this._count = value;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Gets the position of the node relative to its parent.
|
|
162
|
+
* @remarks Time O(1), Space O(1)
|
|
163
|
+
*
|
|
164
|
+
* @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
|
|
165
|
+
*/
|
|
166
|
+
get familyPosition(): FamilyPosition {
|
|
167
|
+
if (!this.parent) {
|
|
168
|
+
return this.left || this.right ? 'ROOT' : 'ISOLATED';
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (this.parent.left === this) {
|
|
172
|
+
return this.left || this.right ? 'ROOT_LEFT' : 'LEFT';
|
|
173
|
+
} else if (this.parent.right === this) {
|
|
174
|
+
return this.left || this.right ? 'ROOT_RIGHT' : 'RIGHT';
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return 'MAL_NODE';
|
|
178
|
+
}
|
|
89
179
|
}
|
|
90
180
|
|
|
91
181
|
/**
|
|
@@ -94,7 +184,7 @@ export class RedBlackTreeNode<K = any, V = any> extends BSTNode<K, V> {
|
|
|
94
184
|
* @template K
|
|
95
185
|
* @template V
|
|
96
186
|
* @template R
|
|
97
|
-
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high but the query efficiency is slightly lower.
|
|
187
|
+
* 1. Efficient self-balancing, but not completely balanced. Compared with AVLTree, the addition and deletion efficiency is high, but the query efficiency is slightly lower.
|
|
98
188
|
* 2. It is BST itself. Compared with Heap which is not completely ordered, RedBlackTree is completely ordered.
|
|
99
189
|
* @example
|
|
100
190
|
* // using Red-Black Tree as a price-based index for stock data
|
|
@@ -380,10 +470,10 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
380
470
|
*/
|
|
381
471
|
|
|
382
472
|
protected _insert(node: RedBlackTreeNode<K, V>): CRUD {
|
|
383
|
-
let current = this.root;
|
|
473
|
+
let current = this.root ?? this.NIL;
|
|
384
474
|
let parent: RedBlackTreeNode<K, V> | undefined = undefined;
|
|
385
475
|
|
|
386
|
-
while (this.
|
|
476
|
+
while (current !== this.NIL) {
|
|
387
477
|
parent = current;
|
|
388
478
|
const compared = this._compare(node.key, current.key);
|
|
389
479
|
if (compared < 0) {
|
|
@@ -459,7 +549,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
459
549
|
this._leftRotate(z);
|
|
460
550
|
}
|
|
461
551
|
|
|
462
|
-
if (z &&
|
|
552
|
+
if (z && z.parent && z.parent.parent) {
|
|
463
553
|
z.parent.color = 'BLACK';
|
|
464
554
|
z.parent.parent.color = 'RED';
|
|
465
555
|
this._rightRotate(z.parent.parent);
|
|
@@ -478,7 +568,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
478
568
|
this._rightRotate(z);
|
|
479
569
|
}
|
|
480
570
|
|
|
481
|
-
if (z &&
|
|
571
|
+
if (z && z.parent && z.parent.parent) {
|
|
482
572
|
z.parent.color = 'BLACK';
|
|
483
573
|
z.parent.parent.color = 'RED';
|
|
484
574
|
this._leftRotate(z.parent.parent);
|
|
@@ -575,7 +665,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
575
665
|
const y = x.right;
|
|
576
666
|
x.right = y.left;
|
|
577
667
|
|
|
578
|
-
if (
|
|
668
|
+
if (y.left && y.left !== this.NIL) {
|
|
579
669
|
y.left.parent = x;
|
|
580
670
|
}
|
|
581
671
|
|
|
@@ -608,7 +698,7 @@ export class RedBlackTree<K = any, V = any, R = any> extends BST<K, V, R> implem
|
|
|
608
698
|
const x = y.left;
|
|
609
699
|
y.left = x.right;
|
|
610
700
|
|
|
611
|
-
if (
|
|
701
|
+
if (x.right && x.right !== this.NIL) {
|
|
612
702
|
x.right.parent = y;
|
|
613
703
|
}
|
|
614
704
|
|