data-structure-typed 1.37.2 → 1.37.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.
- package/CHANGELOG.md +3 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +5 -7
- package/dist/data-structures/binary-tree/avl-tree.js +7 -9
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +36 -20
- package/dist/data-structures/binary-tree/binary-tree.js +66 -50
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +15 -13
- package/dist/data-structures/binary-tree/bst.js +35 -33
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/dist/types/data-structures/binary-tree.d.ts +2 -2
- package/dist/types/data-structures/binary-tree.js +6 -6
- package/dist/types/data-structures/binary-tree.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +5 -7
- package/lib/data-structures/binary-tree/avl-tree.js +7 -9
- package/lib/data-structures/binary-tree/binary-tree.d.ts +36 -20
- package/lib/data-structures/binary-tree/binary-tree.js +67 -51
- package/lib/data-structures/binary-tree/bst.d.ts +15 -13
- package/lib/data-structures/binary-tree/bst.js +36 -34
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +2 -2
- package/lib/data-structures/binary-tree/tree-multiset.js +3 -3
- package/lib/types/data-structures/binary-tree.d.ts +2 -2
- package/lib/types/data-structures/binary-tree.js +5 -5
- package/package.json +6 -6
- package/src/data-structures/binary-tree/avl-tree.ts +7 -9
- package/src/data-structures/binary-tree/binary-tree.ts +79 -54
- package/src/data-structures/binary-tree/bst.ts +37 -32
- package/src/data-structures/binary-tree/tree-multiset.ts +3 -3
- package/src/types/data-structures/binary-tree.ts +2 -2
- package/test/config.ts +1 -0
- package/test/integration/avl-tree.test.ts +7 -8
- package/test/integration/bst.test.ts +17 -16
- package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
- package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
- package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
- package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
- package/test/utils/big-o.ts +2 -1
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CP,
|
|
1
|
+
import { CP, IterationType } from '../../types';
|
|
2
2
|
import { BinaryTree, BinaryTreeNode } from './binary-tree';
|
|
3
3
|
import { Queue } from '../queue';
|
|
4
4
|
export class BSTNode extends BinaryTreeNode {
|
|
@@ -124,9 +124,10 @@ export class BST extends BinaryTree {
|
|
|
124
124
|
* to the binary search tree.
|
|
125
125
|
* @param {N['val'][]} data - The values of tree nodes
|
|
126
126
|
* @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
|
|
127
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a
|
|
127
128
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
128
129
|
*/
|
|
129
|
-
addMany(keysOrNodes, data, isBalanceAdd = true) {
|
|
130
|
+
addMany(keysOrNodes, data, isBalanceAdd = true, iterationType = this.iterationType) {
|
|
130
131
|
// TODO this addMany function is inefficient, it should be optimized
|
|
131
132
|
function hasNoNull(arr) {
|
|
132
133
|
return arr.indexOf(null) === -1;
|
|
@@ -187,7 +188,7 @@ export class BST extends BinaryTree {
|
|
|
187
188
|
}
|
|
188
189
|
}
|
|
189
190
|
};
|
|
190
|
-
if (
|
|
191
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
191
192
|
recursive(sortedKeysOrNodes, sortedData);
|
|
192
193
|
}
|
|
193
194
|
else {
|
|
@@ -208,20 +209,19 @@ export class BST extends BinaryTree {
|
|
|
208
209
|
return (_a = this.getNodes(nodeProperty, callback, true)[0]) !== null && _a !== void 0 ? _a : null;
|
|
209
210
|
}
|
|
210
211
|
/**
|
|
211
|
-
*
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
*
|
|
215
|
-
* equal, it returns the key of the rightmost node. If there are no nodes in the tree, it returns 0.
|
|
212
|
+
* lastKey returns the last key in a binary tree. If the binary tree is empty, it returns 0.
|
|
213
|
+
* @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to begin
|
|
214
|
+
* the search for the last key.
|
|
215
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a recursive or iterative approach to search for the last key.
|
|
216
216
|
*/
|
|
217
|
-
lastKey() {
|
|
217
|
+
lastKey(beginRoot = this.root, iterationType = this.iterationType) {
|
|
218
218
|
var _a, _b, _c, _d, _e, _f;
|
|
219
219
|
if (this._compare(0, 1) === CP.lt)
|
|
220
|
-
return (_b = (_a = this.getRightMost()) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
|
|
220
|
+
return (_b = (_a = this.getRightMost(beginRoot, iterationType)) === null || _a === void 0 ? void 0 : _a.key) !== null && _b !== void 0 ? _b : 0;
|
|
221
221
|
else if (this._compare(0, 1) === CP.gt)
|
|
222
|
-
return (_d = (_c = this.getLeftMost()) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
|
|
222
|
+
return (_d = (_c = this.getLeftMost(beginRoot, iterationType)) === null || _c === void 0 ? void 0 : _c.key) !== null && _d !== void 0 ? _d : 0;
|
|
223
223
|
else
|
|
224
|
-
return (_f = (_e = this.getRightMost()) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
|
|
224
|
+
return (_f = (_e = this.getRightMost(beginRoot, iterationType)) === null || _e === void 0 ? void 0 : _e.key) !== null && _f !== void 0 ? _f : 0;
|
|
225
225
|
}
|
|
226
226
|
/**
|
|
227
227
|
* The function `getNodes` returns an array of nodes in a binary tree that match a given property value.
|
|
@@ -233,13 +233,14 @@ export class BST extends BinaryTree {
|
|
|
233
233
|
* return only one node that matches the given `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne`
|
|
234
234
|
* is set to `true`, the function will return an array with only one node (if
|
|
235
235
|
* @param beginRoot - The `beginRoot` parameter is an optional parameter that specifies the root node from which to
|
|
236
|
+
* @param iterationType
|
|
236
237
|
* @returns an array of nodes (type N).
|
|
237
238
|
*/
|
|
238
|
-
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root) {
|
|
239
|
+
getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
|
|
239
240
|
if (!beginRoot)
|
|
240
241
|
return [];
|
|
241
242
|
const ans = [];
|
|
242
|
-
if (
|
|
243
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
243
244
|
const _traverse = (cur) => {
|
|
244
245
|
const callbackResult = callback(cur);
|
|
245
246
|
if (callbackResult === nodeProperty) {
|
|
@@ -295,45 +296,46 @@ export class BST extends BinaryTree {
|
|
|
295
296
|
* The `lesserOrGreaterTraverse` function adds a delta value to the specified property of all nodes in a binary tree that
|
|
296
297
|
* have a greater value than a given node.
|
|
297
298
|
* @param callback - The `callback` parameter is a function that takes a node as a parameter and returns a value.
|
|
298
|
-
* @param {N | BinaryTreeNodeKey | null} node - The `node` parameter can be either of type `N` (a generic type), `BinaryTreeNodeKey`, or `null`. It
|
|
299
299
|
* represents the node in the binary tree to which the delta value will be added.
|
|
300
300
|
* @param lesserOrGreater - The `lesserOrGreater` parameter is an optional parameter that specifies whether the delta
|
|
301
|
+
* @param targetNode - The `targetNode` parameter is an optional parameter that specifies the node in the binary tree
|
|
302
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies whether to use a
|
|
301
303
|
*/
|
|
302
|
-
lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = CP.lt,
|
|
303
|
-
if (typeof
|
|
304
|
-
|
|
304
|
+
lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = CP.lt, targetNode = this.root, iterationType = this.iterationType) {
|
|
305
|
+
if (typeof targetNode === 'number')
|
|
306
|
+
targetNode = this.get(targetNode);
|
|
305
307
|
const ans = [];
|
|
306
|
-
if (!
|
|
307
|
-
return
|
|
308
|
-
const
|
|
308
|
+
if (!targetNode)
|
|
309
|
+
return ans;
|
|
310
|
+
const targetKey = targetNode.key;
|
|
309
311
|
if (!this.root)
|
|
310
|
-
return
|
|
311
|
-
if (
|
|
312
|
+
return ans;
|
|
313
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
312
314
|
const _traverse = (cur) => {
|
|
313
|
-
const compared = this._compare(cur.key,
|
|
315
|
+
const compared = this._compare(cur.key, targetKey);
|
|
314
316
|
if (compared === lesserOrGreater)
|
|
315
317
|
ans.push(callback(cur));
|
|
316
318
|
if (!cur.left && !cur.right)
|
|
317
319
|
return;
|
|
318
|
-
if (cur.left && this._compare(cur.left.key,
|
|
320
|
+
if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
|
|
319
321
|
_traverse(cur.left);
|
|
320
|
-
if (cur.right && this._compare(cur.right.key,
|
|
322
|
+
if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater)
|
|
321
323
|
_traverse(cur.right);
|
|
322
324
|
};
|
|
323
325
|
_traverse(this.root);
|
|
324
|
-
return
|
|
326
|
+
return ans;
|
|
325
327
|
}
|
|
326
328
|
else {
|
|
327
329
|
const queue = new Queue([this.root]);
|
|
328
330
|
while (queue.size > 0) {
|
|
329
331
|
const cur = queue.shift();
|
|
330
332
|
if (cur) {
|
|
331
|
-
const compared = this._compare(cur.key,
|
|
333
|
+
const compared = this._compare(cur.key, targetKey);
|
|
332
334
|
if (compared === lesserOrGreater)
|
|
333
335
|
ans.push(callback(cur));
|
|
334
|
-
if (cur.left && this._compare(cur.left.key,
|
|
336
|
+
if (cur.left && this._compare(cur.left.key, targetKey) === lesserOrGreater)
|
|
335
337
|
queue.push(cur.left);
|
|
336
|
-
if (cur.right && this._compare(cur.right.key,
|
|
338
|
+
if (cur.right && this._compare(cur.right.key, targetKey) === lesserOrGreater)
|
|
337
339
|
queue.push(cur.right);
|
|
338
340
|
}
|
|
339
341
|
}
|
|
@@ -354,12 +356,12 @@ export class BST extends BinaryTree {
|
|
|
354
356
|
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
355
357
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
356
358
|
*/
|
|
357
|
-
perfectlyBalance() {
|
|
359
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
358
360
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
359
361
|
this.clear();
|
|
360
362
|
if (sorted.length < 1)
|
|
361
363
|
return false;
|
|
362
|
-
if (
|
|
364
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
363
365
|
const buildBalanceBST = (l, r) => {
|
|
364
366
|
if (l > r)
|
|
365
367
|
return;
|
|
@@ -394,12 +396,12 @@ export class BST extends BinaryTree {
|
|
|
394
396
|
* The function `isAVLBalanced` checks if a binary tree is balanced according to the AVL tree property.
|
|
395
397
|
* @returns a boolean value.
|
|
396
398
|
*/
|
|
397
|
-
isAVLBalanced() {
|
|
399
|
+
isAVLBalanced(iterationType = this.iterationType) {
|
|
398
400
|
var _a, _b;
|
|
399
401
|
if (!this.root)
|
|
400
402
|
return true;
|
|
401
403
|
let balanced = true;
|
|
402
|
-
if (
|
|
404
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
403
405
|
const _height = (cur) => {
|
|
404
406
|
if (!cur)
|
|
405
407
|
return 0;
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* @license MIT License
|
|
7
7
|
*/
|
|
8
8
|
import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
|
|
9
|
-
import { BinaryTreeDeletedResult } from '../../types';
|
|
9
|
+
import { BinaryTreeDeletedResult, IterationType } from '../../types';
|
|
10
10
|
import { IBinaryTree } from '../../interfaces';
|
|
11
11
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
12
12
|
export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
|
|
@@ -91,7 +91,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
|
|
|
91
91
|
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
92
92
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
93
93
|
*/
|
|
94
|
-
perfectlyBalance(): boolean;
|
|
94
|
+
perfectlyBalance(iterationType?: IterationType): boolean;
|
|
95
95
|
/**
|
|
96
96
|
* The `delete` function removes a node from a binary search tree and returns the deleted node along with the parent
|
|
97
97
|
* node that needs to be balanced.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { CP, FamilyPosition,
|
|
1
|
+
import { CP, FamilyPosition, IterationType } from '../../types';
|
|
2
2
|
import { AVLTree, AVLTreeNode } from './avl-tree';
|
|
3
3
|
export class TreeMultisetNode extends AVLTreeNode {
|
|
4
4
|
/**
|
|
@@ -220,12 +220,12 @@ export class TreeMultiset extends AVLTree {
|
|
|
220
220
|
* constructs a balanced binary search tree using either a recursive or iterative approach.
|
|
221
221
|
* @returns The function `perfectlyBalance()` returns a boolean value.
|
|
222
222
|
*/
|
|
223
|
-
perfectlyBalance() {
|
|
223
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
224
224
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
225
225
|
if (sorted.length < 1)
|
|
226
226
|
return false;
|
|
227
227
|
this.clear();
|
|
228
|
-
if (
|
|
228
|
+
if (iterationType === IterationType.RECURSIVE) {
|
|
229
229
|
const buildBalanceBST = (l, r) => {
|
|
230
230
|
if (l > r)
|
|
231
231
|
return;
|
|
@@ -5,7 +5,7 @@ import { BinaryTreeNode } from '../../data-structures/binary-tree';
|
|
|
5
5
|
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
6
6
|
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
7
7
|
*/
|
|
8
|
-
export declare enum
|
|
8
|
+
export declare enum IterationType {
|
|
9
9
|
ITERATIVE = "ITERATIVE",
|
|
10
10
|
RECURSIVE = "RECURSIVE"
|
|
11
11
|
}
|
|
@@ -31,5 +31,5 @@ export type BinaryTreeDeletedResult<N> = {
|
|
|
31
31
|
export type BinaryTreeNodeProperties<N extends BinaryTreeNode<N['val'], N>> = BinaryTreeNodeProperty<N>[];
|
|
32
32
|
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>;
|
|
33
33
|
export type BinaryTreeOptions = {
|
|
34
|
-
|
|
34
|
+
iterationType?: IterationType;
|
|
35
35
|
};
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
5
5
|
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
6
6
|
*/
|
|
7
|
-
export var
|
|
8
|
-
(function (
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
})(
|
|
7
|
+
export var IterationType;
|
|
8
|
+
(function (IterationType) {
|
|
9
|
+
IterationType["ITERATIVE"] = "ITERATIVE";
|
|
10
|
+
IterationType["RECURSIVE"] = "RECURSIVE";
|
|
11
|
+
})(IterationType || (IterationType = {}));
|
|
12
12
|
export var FamilyPosition;
|
|
13
13
|
(function (FamilyPosition) {
|
|
14
14
|
FamilyPosition["ROOT"] = "ROOT";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "data-structure-typed",
|
|
3
|
-
"version": "1.37.
|
|
3
|
+
"version": "1.37.3",
|
|
4
4
|
"description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "lib/index.js",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"fix:test": "npm run lint:test && npm run format:test",
|
|
29
29
|
"fix": "npm run fix:src && npm run fix:test",
|
|
30
30
|
"update:individuals": "npm i avl-tree-typed binary-tree-typed bst-typed heap-typed --save-dev",
|
|
31
|
-
"install:individuals": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
|
|
31
|
+
"install:all-individuals": "npm i avl-tree-typed binary-tree-typed bst-typed deque-typed directed-graph-typed doubly-linked-list-typed graph-typed heap-typed linked-list-typed max-heap-typed max-priority-queue-typed min-heap-typed min-priority-queue-typed priority-queue-typed singly-linked-list-typed stack-typed tree-multiset-typed trie-typed undirected-graph-typed queue-typed --save-dev",
|
|
32
32
|
"test": "jest",
|
|
33
33
|
"check:deps": "dependency-cruiser src",
|
|
34
34
|
"changelog": "auto-changelog",
|
|
@@ -58,17 +58,17 @@
|
|
|
58
58
|
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
59
59
|
"@typescript-eslint/parser": "^6.7.4",
|
|
60
60
|
"auto-changelog": "^2.4.0",
|
|
61
|
-
"avl-tree-typed": "^1.37.
|
|
61
|
+
"avl-tree-typed": "^1.37.2",
|
|
62
62
|
"benchmark": "^2.1.4",
|
|
63
|
-
"binary-tree-typed": "^1.37.
|
|
64
|
-
"bst-typed": "^1.37.
|
|
63
|
+
"binary-tree-typed": "^1.37.2",
|
|
64
|
+
"bst-typed": "^1.37.2",
|
|
65
65
|
"dependency-cruiser": "^14.1.0",
|
|
66
66
|
"eslint": "^8.50.0",
|
|
67
67
|
"eslint-config-prettier": "^9.0.0",
|
|
68
68
|
"eslint-import-resolver-alias": "^1.1.2",
|
|
69
69
|
"eslint-import-resolver-typescript": "^3.6.1",
|
|
70
70
|
"eslint-plugin-import": "^2.28.1",
|
|
71
|
-
"heap-typed": "^1.37.
|
|
71
|
+
"heap-typed": "^1.37.2",
|
|
72
72
|
"istanbul-badges-readme": "^1.8.5",
|
|
73
73
|
"jest": "^29.7.0",
|
|
74
74
|
"prettier": "^3.0.3",
|
|
@@ -72,27 +72,25 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
|
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
74
|
* The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
|
|
75
|
-
* @param
|
|
75
|
+
* @param keyOrNode - The `keyOrNode` parameter is either a key or a node that needs to be added to the binary tree.
|
|
76
76
|
* @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
|
|
77
77
|
* `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
|
|
78
78
|
* @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
|
|
79
79
|
*/
|
|
80
|
-
override add(
|
|
80
|
+
override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
|
|
81
81
|
// TODO support node as a param
|
|
82
|
-
const inserted = super.add(
|
|
82
|
+
const inserted = super.add(keyOrNode, val);
|
|
83
83
|
if (inserted) this._balancePath(inserted);
|
|
84
84
|
return inserted;
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
|
-
* The function overrides the delete method of a binary tree and performs additional operations to balance the tree after
|
|
89
|
-
* deletion.
|
|
90
|
-
* @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
|
|
91
|
-
* removed.
|
|
88
|
+
* The function overrides the delete method of a binary tree and performs additional operations to balance the tree after deletion.
|
|
92
89
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
90
|
+
* @param nodeOrKey - The `nodeOrKey` parameter is either a node or a key that needs to be deleted from the binary tree.
|
|
93
91
|
*/
|
|
94
|
-
override delete(
|
|
95
|
-
const deletedResults = super.delete(
|
|
92
|
+
override delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
|
|
93
|
+
const deletedResults = super.delete(nodeOrKey);
|
|
96
94
|
for (const {needBalanced} of deletedResults) {
|
|
97
95
|
if (needBalanced) {
|
|
98
96
|
this._balancePath(needBalanced);
|