avl-tree-typed 1.19.0 → 1.19.3

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.
@@ -0,0 +1,86 @@
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
+ }
@@ -0,0 +1,337 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __values = (this && this.__values) || function(o) {
18
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
19
+ if (m) return m.call(o);
20
+ if (o && typeof o.length === "number") return {
21
+ next: function () {
22
+ if (o && i >= o.length) o = void 0;
23
+ return { value: o && o[i++], done: !o };
24
+ }
25
+ };
26
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.AVLTree = exports.AVLTreeNode = void 0;
30
+ var data_structure_typed_1 = require("data-structure-typed");
31
+ var AVLTreeNode = /** @class */ (function (_super) {
32
+ __extends(AVLTreeNode, _super);
33
+ function AVLTreeNode() {
34
+ return _super !== null && _super.apply(this, arguments) || this;
35
+ }
36
+ return AVLTreeNode;
37
+ }(data_structure_typed_1.BSTNode));
38
+ exports.AVLTreeNode = AVLTreeNode;
39
+ var AVLTree = /** @class */ (function (_super) {
40
+ __extends(AVLTree, _super);
41
+ /**
42
+ * This is a constructor function for an AVL tree data structure in TypeScript.
43
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
44
+ * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
45
+ * options.
46
+ */
47
+ function AVLTree(options) {
48
+ return _super.call(this, options) || this;
49
+ }
50
+ /**
51
+ * The function creates a new AVL tree node with the given id and value.
52
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
53
+ * identify each node in the tree.
54
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
55
+ * that will be stored in the node.
56
+ * @returns a new AVLTreeNode object with the specified id and value.
57
+ */
58
+ AVLTree.prototype.createNode = function (id, val) {
59
+ return new AVLTreeNode(id, val);
60
+ };
61
+ /**
62
+ * The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
63
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
64
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
65
+ * `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
66
+ * @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
67
+ */
68
+ AVLTree.prototype.add = function (id, val) {
69
+ var inserted = _super.prototype.add.call(this, id, val);
70
+ if (inserted)
71
+ this.balancePath(inserted);
72
+ return inserted;
73
+ };
74
+ /**
75
+ * The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
76
+ * then balances the tree if necessary.
77
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
78
+ * removed from the AVL tree.
79
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
80
+ * determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
81
+ * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
82
+ * @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
83
+ */
84
+ AVLTree.prototype.remove = function (id, isUpdateAllLeftSum) {
85
+ var e_1, _a;
86
+ var deletedResults = _super.prototype.remove.call(this, id, isUpdateAllLeftSum);
87
+ try {
88
+ for (var deletedResults_1 = __values(deletedResults), deletedResults_1_1 = deletedResults_1.next(); !deletedResults_1_1.done; deletedResults_1_1 = deletedResults_1.next()) {
89
+ var needBalanced = deletedResults_1_1.value.needBalanced;
90
+ if (needBalanced) {
91
+ this.balancePath(needBalanced);
92
+ }
93
+ }
94
+ }
95
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
96
+ finally {
97
+ try {
98
+ if (deletedResults_1_1 && !deletedResults_1_1.done && (_a = deletedResults_1.return)) _a.call(deletedResults_1);
99
+ }
100
+ finally { if (e_1) throw e_1.error; }
101
+ }
102
+ return deletedResults;
103
+ };
104
+ /**
105
+ * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
106
+ * height of its right subtree.
107
+ * @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
108
+ * @returns The balance factor of the given AVL tree node.
109
+ */
110
+ AVLTree.prototype.balanceFactor = function (node) {
111
+ if (!node.right) // node has no right subtree
112
+ return -node.height;
113
+ else if (!node.left) // node has no left subtree
114
+ return +node.height;
115
+ else
116
+ return node.right.height - node.left.height;
117
+ };
118
+ /**
119
+ * The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
120
+ * @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
121
+ */
122
+ AVLTree.prototype.updateHeight = function (node) {
123
+ if (!node.left && !node.right) // node is a leaf
124
+ node.height = 0;
125
+ else if (!node.left) {
126
+ // node has no left subtree
127
+ var rightHeight = node.right ? node.right.height : 0;
128
+ node.height = 1 + rightHeight;
129
+ }
130
+ else if (!node.right) // node has no right subtree
131
+ node.height = 1 + node.left.height;
132
+ else
133
+ node.height = 1 + Math.max(node.right.height, node.left.height);
134
+ };
135
+ /**
136
+ * The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
137
+ * each node in the path from the given node to the root.
138
+ * @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
139
+ */
140
+ AVLTree.prototype.balancePath = function (node) {
141
+ var path = this.getPathToRoot(node);
142
+ for (var i = path.length - 1; i >= 0; i--) {
143
+ var A = path[i];
144
+ this.updateHeight(A);
145
+ switch (this.balanceFactor(A)) {
146
+ case -2:
147
+ if (A && A.left) {
148
+ if (this.balanceFactor(A.left) <= 0) {
149
+ this.balanceLL(A); // Perform LL rotation
150
+ }
151
+ else {
152
+ this.balanceLR(A); // Perform LR rotation
153
+ }
154
+ }
155
+ break;
156
+ case +2:
157
+ if (A && A.right) {
158
+ if (this.balanceFactor(A.right) >= 0) {
159
+ this.balanceRR(A); // Perform RR rotation
160
+ }
161
+ else {
162
+ this.balanceRL(A); // Perform RL rotation
163
+ }
164
+ }
165
+ }
166
+ }
167
+ };
168
+ /**
169
+ * The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
170
+ * @param A - The parameter A is an AVLTreeNode object.
171
+ */
172
+ AVLTree.prototype.balanceLL = function (A) {
173
+ var parentOfA = A.parent;
174
+ var B = A.left; // A is left-heavy and B is left-heavy
175
+ A.parent = B;
176
+ if (B && B.right) {
177
+ B.right.parent = A;
178
+ }
179
+ if (B)
180
+ B.parent = parentOfA;
181
+ if (A === this.root) {
182
+ if (B)
183
+ this._setRoot(B);
184
+ }
185
+ else {
186
+ if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
187
+ parentOfA.left = B;
188
+ }
189
+ else {
190
+ if (parentOfA)
191
+ parentOfA.right = B;
192
+ }
193
+ }
194
+ if (B) {
195
+ A.left = B.right; // Make T2 the left subtree of A
196
+ B.right = A; // Make A the left child of B
197
+ }
198
+ this.updateHeight(A);
199
+ if (B)
200
+ this.updateHeight(B);
201
+ };
202
+ /**
203
+ * The `balanceLR` function performs a left-right rotation to balance an AVL tree.
204
+ * @param A - A is an AVLTreeNode object.
205
+ */
206
+ AVLTree.prototype.balanceLR = function (A) {
207
+ var parentOfA = A.parent;
208
+ var B = A.left; // A is left-heavy
209
+ var C = null;
210
+ if (B) {
211
+ C = B.right; // B is right-heavy
212
+ }
213
+ if (A)
214
+ A.parent = C;
215
+ if (B)
216
+ B.parent = C;
217
+ if (C) {
218
+ if (C.left) {
219
+ C.left.parent = B;
220
+ }
221
+ if (C.right) {
222
+ C.right.parent = A;
223
+ }
224
+ C.parent = parentOfA;
225
+ }
226
+ if (A === this.root) {
227
+ if (C)
228
+ this._setRoot(C);
229
+ }
230
+ else {
231
+ if (parentOfA) {
232
+ if (parentOfA.left === A) {
233
+ parentOfA.left = C;
234
+ }
235
+ else {
236
+ parentOfA.right = C;
237
+ }
238
+ }
239
+ }
240
+ if (C) {
241
+ A.left = C.right; // Make T3 the left subtree of A
242
+ if (B)
243
+ B.right = C.left; // Make T2 the right subtree of B
244
+ C.left = B;
245
+ C.right = A;
246
+ }
247
+ this.updateHeight(A); // Adjust heights
248
+ B && this.updateHeight(B);
249
+ C && this.updateHeight(C);
250
+ };
251
+ /**
252
+ * The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
253
+ * @param A - The parameter A is an AVLTreeNode object.
254
+ */
255
+ AVLTree.prototype.balanceRR = function (A) {
256
+ var parentOfA = A.parent;
257
+ var B = A.right; // A is right-heavy and B is right-heavy
258
+ A.parent = B;
259
+ if (B) {
260
+ if (B.left) {
261
+ B.left.parent = A;
262
+ }
263
+ B.parent = parentOfA;
264
+ }
265
+ if (A === this.root) {
266
+ if (B)
267
+ this._setRoot(B);
268
+ }
269
+ else {
270
+ if (parentOfA) {
271
+ if (parentOfA.left === A) {
272
+ parentOfA.left = B;
273
+ }
274
+ else {
275
+ parentOfA.right = B;
276
+ }
277
+ }
278
+ }
279
+ if (B) {
280
+ A.right = B.left; // Make T2 the right subtree of A
281
+ B.left = A;
282
+ }
283
+ this.updateHeight(A);
284
+ B && this.updateHeight(B);
285
+ };
286
+ /**
287
+ * The `balanceRL` function performs a right-left rotation to balance an AVL tree.
288
+ * @param A - A is an AVLTreeNode object.
289
+ */
290
+ AVLTree.prototype.balanceRL = function (A) {
291
+ var parentOfA = A.parent;
292
+ var B = A.right; // A is right-heavy
293
+ var C = null;
294
+ if (B) {
295
+ C = B.left; // B is left-heavy
296
+ }
297
+ A.parent = C;
298
+ if (B)
299
+ B.parent = C;
300
+ if (C) {
301
+ if (C.left) {
302
+ C.left.parent = A;
303
+ }
304
+ if (C.right) {
305
+ C.right.parent = B;
306
+ }
307
+ C.parent = parentOfA;
308
+ }
309
+ if (A === this.root) {
310
+ if (C)
311
+ this._setRoot(C);
312
+ }
313
+ else {
314
+ if (parentOfA) {
315
+ if (parentOfA.left === A) {
316
+ parentOfA.left = C;
317
+ }
318
+ else {
319
+ parentOfA.right = C;
320
+ }
321
+ }
322
+ }
323
+ if (C)
324
+ A.right = C.left; // Make T2 the right subtree of A
325
+ if (B && C)
326
+ B.left = C.right; // Make T3 the left subtree of B
327
+ if (C)
328
+ C.left = A;
329
+ if (C)
330
+ C.right = B;
331
+ this.updateHeight(A); // Adjust heights
332
+ B && this.updateHeight(B);
333
+ C && this.updateHeight(C);
334
+ };
335
+ return AVLTree;
336
+ }(data_structure_typed_1.BST));
337
+ exports.AVLTree = AVLTree;
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from './bst';
1
+ export * from './avl-tree';
package/dist/index.js CHANGED
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./bst"), exports);
17
+ __exportStar(require("./avl-tree"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "avl-tree-typed",
3
- "version": "1.19.0",
3
+ "version": "1.19.3",
4
4
  "description": "AVLTree(Adelson-Velsky and Landis Tree). Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -8,11 +8,11 @@
8
8
  "test": "jest",
9
9
  "build:docs": "typedoc --out docs ./src",
10
10
  "deps:check": "dependency-cruiser src",
11
- "build:publish": "npm run test && npm run build && npm run build:docs && npm publish"
11
+ "build:publish": "npm run build && npm run build:docs && npm publish"
12
12
  },
13
13
  "repository": {
14
14
  "type": "git",
15
- "url": "git+https://github.com/zrwusa/bst.git"
15
+ "url": "git+https://github.com/zrwusa/data-structure-typed"
16
16
  },
17
17
  "keywords": [
18
18
  "AVL Tree",
@@ -21,14 +21,27 @@
21
21
  "Binary Tree",
22
22
  "DFS",
23
23
  "DFSIterative",
24
- "BFS"
24
+ "BFS",
25
+ "Self-balancing",
26
+ "Binary search tree",
27
+ "Height-balanced",
28
+ "Rotations",
29
+ "Height difference",
30
+ "Tree rotations",
31
+ "Insertion",
32
+ "Deletion",
33
+ "Balancing factor",
34
+ "Height constraint",
35
+ "Binary sorting tree",
36
+ "Balanced binary search tree",
37
+ "Adelson-Velsky-Landis tree"
25
38
  ],
26
39
  "author": "Tyler Zeng zrwusa@gmail.com",
27
40
  "license": "MIT",
28
41
  "bugs": {
29
- "url": "https://github.com/zrwusa/bst/issues"
42
+ "url": "https://github.com/zrwusa/data-structure-typed/issues"
30
43
  },
31
- "homepage": "https://github.com/zrwusa/bst#readme",
44
+ "homepage": "https://github.com/zrwusa/data-structure-typed#readme",
32
45
  "types": "dist/index.d.ts",
33
46
  "devDependencies": {
34
47
  "@types/jest": "^29.5.3",
@@ -40,7 +53,7 @@
40
53
  "typescript": "^4.9.5"
41
54
  },
42
55
  "dependencies": {
43
- "data-structure-typed": "^1.19.0",
56
+ "data-structure-typed": "^1.19.3",
44
57
  "zod": "^3.22.2"
45
58
  }
46
59
  }
package/dist/bst.d.ts DELETED
@@ -1,119 +0,0 @@
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 { BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTNodeNested, BSTOptions } from 'data-structure-typed';
9
- import { BinaryTree, BinaryTreeNode, CP, IBST, IBSTNode } from 'data-structure-typed';
10
- export declare class BSTNode<T = any, NEIGHBOR extends BSTNode<T, NEIGHBOR> = BSTNodeNested<T>> extends BinaryTreeNode<T, NEIGHBOR> implements IBSTNode<T, NEIGHBOR> {
11
- }
12
- export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBST<N> {
13
- /**
14
- * The constructor function initializes a binary search tree object with an optional comparator function.
15
- * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
16
- */
17
- constructor(options?: BSTOptions);
18
- /**
19
- * The function creates a new binary search 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 binary 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 instance of the BSTNode class with the specified id and value.
25
- */
26
- createNode(id: BinaryTreeNodeId, val?: N['val']): N;
27
- /**
28
- * The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
29
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
30
- * is of type `BinaryTreeNodeId`.
31
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
32
- * the value associated with the node.
33
- * @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
34
- * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
35
- */
36
- add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
37
- /**
38
- * The function returns the first node in a binary tree that matches the given property name and value.
39
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
40
- * generic type `N`. It represents the property of the binary tree node that you want to search for.
41
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
42
- * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
43
- * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
44
- */
45
- get(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName): N | null;
46
- /**
47
- * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
48
- * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
49
- * @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
50
- * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
51
- * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
52
- */
53
- lastKey(): BinaryTreeNodeId;
54
- /**
55
- * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
56
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
57
- * `N` type. It represents the property of the binary tree node that you want to compare with.
58
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
59
- * specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
60
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
61
- * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
62
- * is set to `true`, the function will return an array with only one node (if
63
- * @returns an array of nodes (type N).
64
- */
65
- getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName?: BinaryTreeNodePropertyName, onlyOne?: boolean): N[];
66
- /**
67
- * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
68
- * less than a given node.
69
- * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
70
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
71
- * specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
72
- * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
73
- * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
74
- */
75
- lesserSum(beginNode: N | BinaryTreeNodeId | null, propertyName?: BinaryTreeNodePropertyName): number;
76
- /**
77
- * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
78
- * have a greater value than a given node.
79
- * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
80
- * `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
81
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
82
- * each greater node should be increased.
83
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
84
- * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
85
- * 'id'.
86
- * @returns a boolean value.
87
- */
88
- allGreaterNodesAdd(node: N | BinaryTreeNodeId | null, delta: number, propertyName?: BinaryTreeNodePropertyName): boolean;
89
- /**
90
- * Balancing Adjustment:
91
- * 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.
92
- * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
93
- *
94
- * Use Cases and Efficiency:
95
- * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
96
- * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
97
- */
98
- /**
99
- * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
100
- * constructs a balanced binary search tree using either a recursive or iterative approach.
101
- * @returns The function `perfectlyBalance()` returns a boolean value.
102
- */
103
- perfectlyBalance(): boolean;
104
- /**
105
- * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
106
- * @returns a boolean value.
107
- */
108
- isAVLBalanced(): boolean;
109
- protected _comparator: BSTComparator;
110
- /**
111
- * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
112
- * greater than, less than, or equal to the second ID.
113
- * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
114
- * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
115
- * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
116
- * than), or CP.eq (equal).
117
- */
118
- protected _compare(a: BinaryTreeNodeId, b: BinaryTreeNodeId): CP;
119
- }
package/dist/bst.js DELETED
@@ -1,507 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- Object.defineProperty(exports, "__esModule", { value: true });
34
- exports.BST = exports.BSTNode = void 0;
35
- var data_structure_typed_1 = require("data-structure-typed");
36
- var BSTNode = /** @class */ (function (_super) {
37
- __extends(BSTNode, _super);
38
- function BSTNode() {
39
- return _super !== null && _super.apply(this, arguments) || this;
40
- }
41
- return BSTNode;
42
- }(data_structure_typed_1.BinaryTreeNode));
43
- exports.BSTNode = BSTNode;
44
- var BST = /** @class */ (function (_super) {
45
- __extends(BST, _super);
46
- /**
47
- * The constructor function initializes a binary search tree object with an optional comparator function.
48
- * @param {BSTOptions} [options] - An optional object that contains configuration options for the binary search tree.
49
- */
50
- function BST(options) {
51
- var _this = _super.call(this, options) || this;
52
- _this._comparator = function (a, b) { return a - b; };
53
- if (options !== undefined) {
54
- var comparator = options.comparator;
55
- if (comparator !== undefined) {
56
- _this._comparator = comparator;
57
- }
58
- }
59
- return _this;
60
- }
61
- /**
62
- * The function creates a new binary search tree node with the given id and value.
63
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
64
- * identify each node in the binary tree.
65
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
66
- * that will be stored in the node.
67
- * @returns a new instance of the BSTNode class with the specified id and value.
68
- */
69
- BST.prototype.createNode = function (id, val) {
70
- return new BSTNode(id, val);
71
- };
72
- /**
73
- * The `add` function adds a new node to a binary tree, ensuring that duplicates are not accepted.
74
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add. It
75
- * is of type `BinaryTreeNodeId`.
76
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It represents
77
- * the value associated with the node.
78
- * @returns The function `add` returns the inserted node (`inserted`) if it was successfully added to the binary tree.
79
- * If the node was not added (e.g., due to a duplicate ID), it returns `null` or `undefined`.
80
- */
81
- BST.prototype.add = function (id, val) {
82
- var inserted = null;
83
- var newNode = this.createNode(id, val);
84
- if (this.root === null) {
85
- this._setRoot(newNode);
86
- this._setSize(this.size + 1);
87
- inserted = (this.root);
88
- }
89
- else {
90
- var cur = this.root;
91
- var traversing = true;
92
- while (traversing) {
93
- if (cur !== null && newNode !== null) {
94
- if (this._compare(cur.id, id) === data_structure_typed_1.CP.eq) {
95
- if (newNode) {
96
- cur.val = newNode.val;
97
- }
98
- //Duplicates are not accepted.
99
- traversing = false;
100
- inserted = cur;
101
- }
102
- else if (this._compare(cur.id, id) === data_structure_typed_1.CP.gt) {
103
- // Traverse left of the node
104
- if (cur.left === undefined) {
105
- if (newNode) {
106
- newNode.parent = cur;
107
- }
108
- //Add to the left of the current node
109
- cur.left = newNode;
110
- this._setSize(this.size + 1);
111
- traversing = false;
112
- inserted = cur.left;
113
- }
114
- else {
115
- //Traverse the left of the current node
116
- if (cur.left)
117
- cur = cur.left;
118
- }
119
- }
120
- else if (this._compare(cur.id, id) === data_structure_typed_1.CP.lt) {
121
- // Traverse right of the node
122
- if (cur.right === undefined) {
123
- if (newNode) {
124
- newNode.parent = cur;
125
- }
126
- //Add to the right of the current node
127
- cur.right = newNode;
128
- this._setSize(this.size + 1);
129
- traversing = false;
130
- inserted = (cur.right);
131
- }
132
- else {
133
- //Traverse the left of the current node
134
- if (cur.right)
135
- cur = cur.right;
136
- }
137
- }
138
- }
139
- else {
140
- traversing = false;
141
- }
142
- }
143
- }
144
- return inserted;
145
- };
146
- /**
147
- * The function returns the first node in a binary tree that matches the given property name and value.
148
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
149
- * generic type `N`. It represents the property of the binary tree node that you want to search for.
150
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
151
- * specifies the property name to use for searching the binary tree nodes. If not provided, it defaults to `'id'`.
152
- * @returns The method is returning either a BinaryTreeNodeId or N (generic type) or null.
153
- */
154
- BST.prototype.get = function (nodeProperty, propertyName) {
155
- var _a;
156
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
157
- return (_a = this.getNodes(nodeProperty, propertyName, true)[0]) !== null && _a !== void 0 ? _a : null;
158
- };
159
- /**
160
- * The function returns the id of the rightmost node if the comparison between two values is less than, the id of the
161
- * leftmost node if the comparison is greater than, and the id of the rightmost node otherwise.
162
- * @returns The method `lastKey()` returns the id of the rightmost node in the binary tree if the comparison between
163
- * the values at index 0 and 1 is less than, otherwise it returns the id of the leftmost node. If the comparison is
164
- * equal, it returns the id of the rightmost node. If there are no nodes in the tree, it returns 0.
165
- */
166
- BST.prototype.lastKey = function () {
167
- var _a, _b, _c, _d, _e, _f;
168
- if (this._compare(0, 1) === data_structure_typed_1.CP.lt)
169
- return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : 0;
170
- else if (this._compare(0, 1) === data_structure_typed_1.CP.gt)
171
- return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 0;
172
- else
173
- return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.id) !== null && _f !== void 0 ? _f : 0;
174
- };
175
- /**
176
- * The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
177
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or an
178
- * `N` type. It represents the property of the binary tree node that you want to compare with.
179
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
180
- * specifies the property name to use for comparison. If not provided, it defaults to `'id'`.
181
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
182
- * return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
183
- * is set to `true`, the function will return an array with only one node (if
184
- * @returns an array of nodes (type N).
185
- */
186
- BST.prototype.getNodes = function (nodeProperty, propertyName, onlyOne) {
187
- var _this = this;
188
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
189
- if (!this.root)
190
- return [];
191
- var result = [];
192
- if (this.loopType === data_structure_typed_1.LoopType.RECURSIVE) {
193
- var _traverse_1 = function (cur) {
194
- if (_this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
195
- return;
196
- if (!cur.left && !cur.right)
197
- return;
198
- if (propertyName === 'id') {
199
- if (_this._compare(cur.id, nodeProperty) === data_structure_typed_1.CP.gt)
200
- cur.left && _traverse_1(cur.left);
201
- if (_this._compare(cur.id, nodeProperty) === data_structure_typed_1.CP.lt)
202
- cur.right && _traverse_1(cur.right);
203
- }
204
- else {
205
- cur.left && _traverse_1(cur.left);
206
- cur.right && _traverse_1(cur.right);
207
- }
208
- };
209
- _traverse_1(this.root);
210
- }
211
- else {
212
- var queue = [this.root];
213
- while (queue.length > 0) {
214
- var cur = queue.shift();
215
- if (cur) {
216
- if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne))
217
- return result;
218
- if (propertyName === 'id') {
219
- if (this._compare(cur.id, nodeProperty) === data_structure_typed_1.CP.gt)
220
- cur.left && queue.push(cur.left);
221
- if (this._compare(cur.id, nodeProperty) === data_structure_typed_1.CP.lt)
222
- cur.right && queue.push(cur.right);
223
- }
224
- else {
225
- cur.left && queue.push(cur.left);
226
- cur.right && queue.push(cur.right);
227
- }
228
- }
229
- }
230
- }
231
- return result;
232
- };
233
- // --- start additional functions
234
- /**
235
- * The `lesserSum` function calculates the sum of property values in a binary tree for nodes that have a property value
236
- * less than a given node.
237
- * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
238
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
239
- * specifies the property name to use for calculating the sum. If not provided, it defaults to `'id'`.
240
- * @returns The function `lesserSum` returns a number, which represents the sum of the values of the nodes in the
241
- * binary tree that have a lesser value than the specified `beginNode` based on the `propertyName`.
242
- */
243
- BST.prototype.lesserSum = function (beginNode, propertyName) {
244
- var _this = this;
245
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
246
- if (typeof beginNode === 'number')
247
- beginNode = this.get(beginNode, 'id');
248
- if (!beginNode)
249
- return 0;
250
- if (!this.root)
251
- return 0;
252
- var id = beginNode.id;
253
- var getSumByPropertyName = function (cur) {
254
- var needSum;
255
- switch (propertyName) {
256
- case 'id':
257
- needSum = cur.id;
258
- break;
259
- default:
260
- needSum = cur.id;
261
- break;
262
- }
263
- return needSum;
264
- };
265
- var sum = 0;
266
- if (this.loopType === data_structure_typed_1.LoopType.RECURSIVE) {
267
- var _traverse_2 = function (cur) {
268
- var compared = _this._compare(cur.id, id);
269
- if (compared === data_structure_typed_1.CP.eq) {
270
- if (cur.right)
271
- sum += _this.subTreeSum(cur.right, propertyName);
272
- return;
273
- }
274
- else if (compared === data_structure_typed_1.CP.lt) {
275
- if (cur.left)
276
- sum += _this.subTreeSum(cur.left, propertyName);
277
- sum += getSumByPropertyName(cur);
278
- if (cur.right)
279
- _traverse_2(cur.right);
280
- else
281
- return;
282
- }
283
- else {
284
- if (cur.left)
285
- _traverse_2(cur.left);
286
- else
287
- return;
288
- }
289
- };
290
- _traverse_2(this.root);
291
- }
292
- else {
293
- var queue = [this.root];
294
- while (queue.length > 0) {
295
- var cur = queue.shift();
296
- if (cur) {
297
- var compared = this._compare(cur.id, id);
298
- if (compared === data_structure_typed_1.CP.eq) {
299
- if (cur.right)
300
- sum += this.subTreeSum(cur.right, propertyName);
301
- return sum;
302
- }
303
- else if (compared === data_structure_typed_1.CP.lt) { // todo maybe a bug
304
- if (cur.left)
305
- sum += this.subTreeSum(cur.left, propertyName);
306
- sum += getSumByPropertyName(cur);
307
- if (cur.right)
308
- queue.push(cur.right);
309
- else
310
- return sum;
311
- }
312
- else {
313
- if (cur.left)
314
- queue.push(cur.left);
315
- else
316
- return sum;
317
- }
318
- }
319
- }
320
- }
321
- return sum;
322
- };
323
- /**
324
- * The `allGreaterNodesAdd` function adds a delta value to the specified property of all nodes in a binary tree that
325
- * have a greater value than a given node.
326
- * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be either of type `N` (a generic type),
327
- * `BinaryTreeNodeId`, or `null`. It represents the node in the binary tree to which the delta value will be added.
328
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
329
- * each greater node should be increased.
330
- * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
331
- * specifies the property name of the nodes in the binary tree that you want to update. If not provided, it defaults to
332
- * 'id'.
333
- * @returns a boolean value.
334
- */
335
- BST.prototype.allGreaterNodesAdd = function (node, delta, propertyName) {
336
- var _this = this;
337
- propertyName = propertyName !== null && propertyName !== void 0 ? propertyName : 'id';
338
- if (typeof node === 'number')
339
- node = this.get(node, 'id');
340
- if (!node)
341
- return false;
342
- var id = node.id;
343
- if (!this.root)
344
- return false;
345
- var _sumByPropertyName = function (cur) {
346
- switch (propertyName) {
347
- case 'id':
348
- cur.id += delta;
349
- break;
350
- default:
351
- cur.id += delta;
352
- break;
353
- }
354
- };
355
- if (this.loopType === data_structure_typed_1.LoopType.RECURSIVE) {
356
- var _traverse_3 = function (cur) {
357
- var compared = _this._compare(cur.id, id);
358
- if (compared === data_structure_typed_1.CP.gt)
359
- _sumByPropertyName(cur);
360
- if (!cur.left && !cur.right)
361
- return;
362
- if (cur.left && _this._compare(cur.left.id, id) === data_structure_typed_1.CP.gt)
363
- _traverse_3(cur.left);
364
- if (cur.right && _this._compare(cur.right.id, id) === data_structure_typed_1.CP.gt)
365
- _traverse_3(cur.right);
366
- };
367
- _traverse_3(this.root);
368
- return true;
369
- }
370
- else {
371
- var queue = [this.root];
372
- while (queue.length > 0) {
373
- var cur = queue.shift();
374
- if (cur) {
375
- var compared = this._compare(cur.id, id);
376
- if (compared === data_structure_typed_1.CP.gt)
377
- _sumByPropertyName(cur);
378
- if (cur.left && this._compare(cur.left.id, id) === data_structure_typed_1.CP.gt)
379
- queue.push(cur.left);
380
- if (cur.right && this._compare(cur.right.id, id) === data_structure_typed_1.CP.gt)
381
- queue.push(cur.right);
382
- }
383
- }
384
- return true;
385
- }
386
- };
387
- /**
388
- * Balancing Adjustment:
389
- * 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.
390
- * AVL Tree: After insertion or deletion operations, an AVL tree performs rotation adjustments based on the balance factor of nodes to restore the tree's balance. These rotations can be left rotations, right rotations, left-right rotations, or right-left rotations, performed as needed.
391
- *
392
- * Use Cases and Efficiency:
393
- * Perfectly Balanced Binary Tree: Perfectly balanced binary trees are typically used in specific scenarios such as complete binary heaps in heap sort or certain types of Huffman trees. However, they are not suitable for dynamic operations requiring frequent insertions and deletions, as these operations often necessitate full tree reconstruction.
394
- * AVL Tree: AVL trees are well-suited for scenarios involving frequent searching, insertion, and deletion operations. Through rotation adjustments, AVL trees maintain their balance, ensuring average and worst-case time complexity of O(log n).
395
- */
396
- /**
397
- * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
398
- * constructs a balanced binary search tree using either a recursive or iterative approach.
399
- * @returns The function `perfectlyBalance()` returns a boolean value.
400
- */
401
- BST.prototype.perfectlyBalance = function () {
402
- var _this = this;
403
- var sorted = this.DFS('in', 'node'), n = sorted.length;
404
- this.clear();
405
- if (sorted.length < 1)
406
- return false;
407
- if (this.loopType === data_structure_typed_1.LoopType.RECURSIVE) {
408
- var buildBalanceBST_1 = function (l, r) {
409
- if (l > r)
410
- return;
411
- var m = l + Math.floor((r - l) / 2);
412
- var midNode = sorted[m];
413
- _this.add(midNode.id, midNode.val);
414
- buildBalanceBST_1(l, m - 1);
415
- buildBalanceBST_1(m + 1, r);
416
- };
417
- buildBalanceBST_1(0, n - 1);
418
- return true;
419
- }
420
- else {
421
- var stack = [[0, n - 1]];
422
- while (stack.length > 0) {
423
- var popped = stack.pop();
424
- if (popped) {
425
- var _a = __read(popped, 2), l = _a[0], r = _a[1];
426
- if (l <= r) {
427
- var m = l + Math.floor((r - l) / 2);
428
- var midNode = sorted[m];
429
- this.add(midNode.id, midNode.val);
430
- stack.push([m + 1, r]);
431
- stack.push([l, m - 1]);
432
- }
433
- }
434
- }
435
- return true;
436
- }
437
- };
438
- /**
439
- * The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
440
- * @returns a boolean value.
441
- */
442
- BST.prototype.isAVLBalanced = function () {
443
- var _a, _b;
444
- if (!this.root)
445
- return true;
446
- var balanced = true;
447
- if (this.loopType === data_structure_typed_1.LoopType.RECURSIVE) {
448
- var _height_1 = function (cur) {
449
- if (!cur)
450
- return 0;
451
- var leftHeight = _height_1(cur.left), rightHeight = _height_1(cur.right);
452
- if (Math.abs(leftHeight - rightHeight) > 1)
453
- balanced = false;
454
- return Math.max(leftHeight, rightHeight) + 1;
455
- };
456
- _height_1(this.root);
457
- }
458
- else {
459
- var stack = [];
460
- var node = this.root, last = null;
461
- var depths = new Map();
462
- while (stack.length > 0 || node) {
463
- if (node) {
464
- stack.push(node);
465
- node = node.left;
466
- }
467
- else {
468
- node = stack[stack.length - 1];
469
- if (!node.right || last === node.right) {
470
- node = stack.pop();
471
- if (node) {
472
- var left = node.left ? (_a = depths.get(node.left)) !== null && _a !== void 0 ? _a : -1 : -1;
473
- var right = node.right ? (_b = depths.get(node.right)) !== null && _b !== void 0 ? _b : -1 : -1;
474
- if (Math.abs(left - right) > 1)
475
- return false;
476
- depths.set(node, 1 + Math.max(left, right));
477
- last = node;
478
- node = null;
479
- }
480
- }
481
- else
482
- node = node.right;
483
- }
484
- }
485
- }
486
- return balanced;
487
- };
488
- /**
489
- * The function compares two binary tree node IDs using a comparator function and returns whether the first ID is
490
- * greater than, less than, or equal to the second ID.
491
- * @param {BinaryTreeNodeId} a - a is a BinaryTreeNodeId, which represents the identifier of a binary tree node.
492
- * @param {BinaryTreeNodeId} b - The parameter "b" in the above code refers to a BinaryTreeNodeId.
493
- * @returns a value of type CP (ComparisonResult). The possible return values are CP.gt (greater than), CP.lt (less
494
- * than), or CP.eq (equal).
495
- */
496
- BST.prototype._compare = function (a, b) {
497
- var compared = this._comparator(a, b);
498
- if (compared > 0)
499
- return data_structure_typed_1.CP.gt;
500
- else if (compared < 0)
501
- return data_structure_typed_1.CP.lt;
502
- else
503
- return data_structure_typed_1.CP.eq;
504
- };
505
- return BST;
506
- }(data_structure_typed_1.BinaryTree));
507
- exports.BST = BST;