avl-tree-typed 1.19.42 → 1.19.43
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/index.d.ts +86 -1
- package/dist/index.js +291 -4
- package/package.json +1 -1
- package/tsconfig.json +5 -3
package/dist/index.d.ts
CHANGED
|
@@ -1 +1,86 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* data-structure-typed
|
|
3
|
+
*
|
|
4
|
+
* @author Tyler Zeng
|
|
5
|
+
* @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
|
|
6
|
+
* @license MIT License
|
|
7
|
+
*/
|
|
8
|
+
import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId } from 'data-structure-typed';
|
|
9
|
+
import { BST, BSTNode, IAVLTree, IAVLTreeNode } from 'data-structure-typed';
|
|
10
|
+
export declare class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
|
|
11
|
+
}
|
|
12
|
+
export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
|
|
13
|
+
/**
|
|
14
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
15
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
16
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
17
|
+
* options.
|
|
18
|
+
*/
|
|
19
|
+
constructor(options?: AVLTreeOptions);
|
|
20
|
+
/**
|
|
21
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
22
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
23
|
+
* identify each node in the tree.
|
|
24
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
25
|
+
* that will be stored in the node.
|
|
26
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
27
|
+
*/
|
|
28
|
+
createNode(id: BinaryTreeNodeId, val?: N['val']): N;
|
|
29
|
+
/**
|
|
30
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
31
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
32
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
33
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
34
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
35
|
+
*/
|
|
36
|
+
add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
39
|
+
* then balances the tree if necessary.
|
|
40
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
41
|
+
* removed from the AVL tree.
|
|
42
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
43
|
+
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
44
|
+
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
45
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
46
|
+
*/
|
|
47
|
+
remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[];
|
|
48
|
+
/**
|
|
49
|
+
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
50
|
+
* height of its right subtree.
|
|
51
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
52
|
+
* @returns The balance factor of the given AVL tree node.
|
|
53
|
+
*/
|
|
54
|
+
balanceFactor(node: N): number;
|
|
55
|
+
/**
|
|
56
|
+
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
57
|
+
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
58
|
+
*/
|
|
59
|
+
updateHeight(node: N): void;
|
|
60
|
+
/**
|
|
61
|
+
* The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
62
|
+
* each node in the path from the given node to the root.
|
|
63
|
+
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
64
|
+
*/
|
|
65
|
+
balancePath(node: N): void;
|
|
66
|
+
/**
|
|
67
|
+
* The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
68
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
69
|
+
*/
|
|
70
|
+
balanceLL(A: N): void;
|
|
71
|
+
/**
|
|
72
|
+
* The `balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
73
|
+
* @param A - A is an AVLTreeNode object.
|
|
74
|
+
*/
|
|
75
|
+
balanceLR(A: N): void;
|
|
76
|
+
/**
|
|
77
|
+
* The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
78
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
79
|
+
*/
|
|
80
|
+
balanceRR(A: N): void;
|
|
81
|
+
/**
|
|
82
|
+
* The `balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
83
|
+
* @param A - A is an AVLTreeNode object.
|
|
84
|
+
*/
|
|
85
|
+
balanceRL(A: N): void;
|
|
86
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,293 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
exports.AVLTree = exports.AVLTreeNode = void 0;
|
|
4
|
+
const data_structure_typed_1 = require("data-structure-typed");
|
|
5
|
+
class AVLTreeNode extends data_structure_typed_1.BSTNode {
|
|
6
|
+
}
|
|
7
|
+
exports.AVLTreeNode = AVLTreeNode;
|
|
8
|
+
class AVLTree extends data_structure_typed_1.BST {
|
|
9
|
+
/**
|
|
10
|
+
* This is a constructor function for an AVL tree data structure in TypeScript.
|
|
11
|
+
* @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
|
|
12
|
+
* constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
|
|
13
|
+
* options.
|
|
14
|
+
*/
|
|
15
|
+
constructor(options) {
|
|
16
|
+
super(options);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The function creates a new AVL tree node with the given id and value.
|
|
20
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
|
|
21
|
+
* identify each node in the tree.
|
|
22
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
|
|
23
|
+
* that will be stored in the node.
|
|
24
|
+
* @returns a new AVLTreeNode object with the specified id and value.
|
|
25
|
+
*/
|
|
26
|
+
createNode(id, val) {
|
|
27
|
+
return new AVLTreeNode(id, val);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
31
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
|
|
32
|
+
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
33
|
+
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
34
|
+
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
35
|
+
*/
|
|
36
|
+
add(id, val) {
|
|
37
|
+
const inserted = super.add(id, val);
|
|
38
|
+
if (inserted)
|
|
39
|
+
this.balancePath(inserted);
|
|
40
|
+
return inserted;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
|
|
44
|
+
* then balances the tree if necessary.
|
|
45
|
+
* @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
|
|
46
|
+
* removed from the AVL tree.
|
|
47
|
+
* @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
|
|
48
|
+
* determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
|
|
49
|
+
* `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
|
|
50
|
+
* @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
|
|
51
|
+
*/
|
|
52
|
+
remove(id, isUpdateAllLeftSum) {
|
|
53
|
+
const deletedResults = super.remove(id, isUpdateAllLeftSum);
|
|
54
|
+
for (const { needBalanced } of deletedResults) {
|
|
55
|
+
if (needBalanced) {
|
|
56
|
+
this.balancePath(needBalanced);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return deletedResults;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
|
|
63
|
+
* height of its right subtree.
|
|
64
|
+
* @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
|
|
65
|
+
* @returns The balance factor of the given AVL tree node.
|
|
66
|
+
*/
|
|
67
|
+
balanceFactor(node) {
|
|
68
|
+
if (!node.right) // node has no right subtree
|
|
69
|
+
return -node.height;
|
|
70
|
+
else if (!node.left) // node has no left subtree
|
|
71
|
+
return +node.height;
|
|
72
|
+
else
|
|
73
|
+
return node.right.height - node.left.height;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
|
|
77
|
+
* @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
78
|
+
*/
|
|
79
|
+
updateHeight(node) {
|
|
80
|
+
if (!node.left && !node.right) // node is a leaf
|
|
81
|
+
node.height = 0;
|
|
82
|
+
else if (!node.left) {
|
|
83
|
+
// node has no left subtree
|
|
84
|
+
const rightHeight = node.right ? node.right.height : 0;
|
|
85
|
+
node.height = 1 + rightHeight;
|
|
86
|
+
}
|
|
87
|
+
else if (!node.right) // node has no right subtree
|
|
88
|
+
node.height = 1 + node.left.height;
|
|
89
|
+
else
|
|
90
|
+
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
|
|
94
|
+
* each node in the path from the given node to the root.
|
|
95
|
+
* @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
|
|
96
|
+
*/
|
|
97
|
+
balancePath(node) {
|
|
98
|
+
const path = this.getPathToRoot(node);
|
|
99
|
+
for (let i = path.length - 1; i >= 0; i--) {
|
|
100
|
+
const A = path[i];
|
|
101
|
+
this.updateHeight(A);
|
|
102
|
+
switch (this.balanceFactor(A)) {
|
|
103
|
+
case -2:
|
|
104
|
+
if (A && A.left) {
|
|
105
|
+
if (this.balanceFactor(A.left) <= 0) {
|
|
106
|
+
this.balanceLL(A); // Perform LL rotation
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
this.balanceLR(A); // Perform LR rotation
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
case +2:
|
|
114
|
+
if (A && A.right) {
|
|
115
|
+
if (this.balanceFactor(A.right) >= 0) {
|
|
116
|
+
this.balanceRR(A); // Perform RR rotation
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
this.balanceRL(A); // Perform RL rotation
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
|
|
127
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
128
|
+
*/
|
|
129
|
+
balanceLL(A) {
|
|
130
|
+
const parentOfA = A.parent;
|
|
131
|
+
const B = A.left; // A is left-heavy and B is left-heavy
|
|
132
|
+
A.parent = B;
|
|
133
|
+
if (B && B.right) {
|
|
134
|
+
B.right.parent = A;
|
|
135
|
+
}
|
|
136
|
+
if (B)
|
|
137
|
+
B.parent = parentOfA;
|
|
138
|
+
if (A === this.root) {
|
|
139
|
+
if (B)
|
|
140
|
+
this._setRoot(B);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
|
|
144
|
+
parentOfA.left = B;
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
if (parentOfA)
|
|
148
|
+
parentOfA.right = B;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (B) {
|
|
152
|
+
A.left = B.right; // Make T2 the left subtree of A
|
|
153
|
+
B.right = A; // Make A the left child of B
|
|
154
|
+
}
|
|
155
|
+
this.updateHeight(A);
|
|
156
|
+
if (B)
|
|
157
|
+
this.updateHeight(B);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* The `balanceLR` function performs a left-right rotation to balance an AVL tree.
|
|
161
|
+
* @param A - A is an AVLTreeNode object.
|
|
162
|
+
*/
|
|
163
|
+
balanceLR(A) {
|
|
164
|
+
const parentOfA = A.parent;
|
|
165
|
+
const B = A.left; // A is left-heavy
|
|
166
|
+
let C = null;
|
|
167
|
+
if (B) {
|
|
168
|
+
C = B.right; // B is right-heavy
|
|
169
|
+
}
|
|
170
|
+
if (A)
|
|
171
|
+
A.parent = C;
|
|
172
|
+
if (B)
|
|
173
|
+
B.parent = C;
|
|
174
|
+
if (C) {
|
|
175
|
+
if (C.left) {
|
|
176
|
+
C.left.parent = B;
|
|
177
|
+
}
|
|
178
|
+
if (C.right) {
|
|
179
|
+
C.right.parent = A;
|
|
180
|
+
}
|
|
181
|
+
C.parent = parentOfA;
|
|
182
|
+
}
|
|
183
|
+
if (A === this.root) {
|
|
184
|
+
if (C)
|
|
185
|
+
this._setRoot(C);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
if (parentOfA) {
|
|
189
|
+
if (parentOfA.left === A) {
|
|
190
|
+
parentOfA.left = C;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
parentOfA.right = C;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (C) {
|
|
198
|
+
A.left = C.right; // Make T3 the left subtree of A
|
|
199
|
+
if (B)
|
|
200
|
+
B.right = C.left; // Make T2 the right subtree of B
|
|
201
|
+
C.left = B;
|
|
202
|
+
C.right = A;
|
|
203
|
+
}
|
|
204
|
+
this.updateHeight(A); // Adjust heights
|
|
205
|
+
B && this.updateHeight(B);
|
|
206
|
+
C && this.updateHeight(C);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
|
|
210
|
+
* @param A - The parameter A is an AVLTreeNode object.
|
|
211
|
+
*/
|
|
212
|
+
balanceRR(A) {
|
|
213
|
+
const parentOfA = A.parent;
|
|
214
|
+
const B = A.right; // A is right-heavy and B is right-heavy
|
|
215
|
+
A.parent = B;
|
|
216
|
+
if (B) {
|
|
217
|
+
if (B.left) {
|
|
218
|
+
B.left.parent = A;
|
|
219
|
+
}
|
|
220
|
+
B.parent = parentOfA;
|
|
221
|
+
}
|
|
222
|
+
if (A === this.root) {
|
|
223
|
+
if (B)
|
|
224
|
+
this._setRoot(B);
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
if (parentOfA) {
|
|
228
|
+
if (parentOfA.left === A) {
|
|
229
|
+
parentOfA.left = B;
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
parentOfA.right = B;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (B) {
|
|
237
|
+
A.right = B.left; // Make T2 the right subtree of A
|
|
238
|
+
B.left = A;
|
|
239
|
+
}
|
|
240
|
+
this.updateHeight(A);
|
|
241
|
+
B && this.updateHeight(B);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* The `balanceRL` function performs a right-left rotation to balance an AVL tree.
|
|
245
|
+
* @param A - A is an AVLTreeNode object.
|
|
246
|
+
*/
|
|
247
|
+
balanceRL(A) {
|
|
248
|
+
const parentOfA = A.parent;
|
|
249
|
+
const B = A.right; // A is right-heavy
|
|
250
|
+
let C = null;
|
|
251
|
+
if (B) {
|
|
252
|
+
C = B.left; // B is left-heavy
|
|
253
|
+
}
|
|
254
|
+
A.parent = C;
|
|
255
|
+
if (B)
|
|
256
|
+
B.parent = C;
|
|
257
|
+
if (C) {
|
|
258
|
+
if (C.left) {
|
|
259
|
+
C.left.parent = A;
|
|
260
|
+
}
|
|
261
|
+
if (C.right) {
|
|
262
|
+
C.right.parent = B;
|
|
263
|
+
}
|
|
264
|
+
C.parent = parentOfA;
|
|
265
|
+
}
|
|
266
|
+
if (A === this.root) {
|
|
267
|
+
if (C)
|
|
268
|
+
this._setRoot(C);
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
if (parentOfA) {
|
|
272
|
+
if (parentOfA.left === A) {
|
|
273
|
+
parentOfA.left = C;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
parentOfA.right = C;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
if (C)
|
|
281
|
+
A.right = C.left; // Make T2 the right subtree of A
|
|
282
|
+
if (B && C)
|
|
283
|
+
B.left = C.right; // Make T3 the left subtree of B
|
|
284
|
+
if (C)
|
|
285
|
+
C.left = A;
|
|
286
|
+
if (C)
|
|
287
|
+
C.right = B;
|
|
288
|
+
this.updateHeight(A); // Adjust heights
|
|
289
|
+
B && this.updateHeight(B);
|
|
290
|
+
C && this.updateHeight(C);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
exports.AVLTree = AVLTree;
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
"module": "commonjs",
|
|
6
6
|
"target": "es6",
|
|
7
7
|
"lib": [
|
|
8
|
-
"
|
|
8
|
+
// "es2015",
|
|
9
|
+
"esnext"
|
|
9
10
|
],
|
|
10
11
|
"strict": true,
|
|
11
12
|
"esModuleInterop": true,
|
|
@@ -30,8 +31,9 @@
|
|
|
30
31
|
"src",
|
|
31
32
|
],
|
|
32
33
|
"exclude": [
|
|
33
|
-
// "node_modules/data-structure-typed",
|
|
34
|
+
// "node_modules/data-structure-typed",
|
|
34
35
|
"node_modules",
|
|
35
36
|
"dist"
|
|
36
37
|
]
|
|
37
|
-
}
|
|
38
|
+
}
|
|
39
|
+
|