data-structure-typed 1.37.2 → 1.37.4
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 +44 -38
- package/dist/data-structures/binary-tree/avl-tree.js +46 -40
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +305 -192
- package/dist/data-structures/binary-tree/binary-tree.js +304 -201
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +111 -64
- package/dist/data-structures/binary-tree/bst.js +132 -85
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +49 -41
- package/dist/data-structures/binary-tree/tree-multiset.js +49 -41
- 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 +44 -38
- package/lib/data-structures/binary-tree/avl-tree.js +46 -40
- package/lib/data-structures/binary-tree/binary-tree.d.ts +305 -192
- package/lib/data-structures/binary-tree/binary-tree.js +305 -202
- package/lib/data-structures/binary-tree/bst.d.ts +111 -64
- package/lib/data-structures/binary-tree/bst.js +133 -86
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +49 -41
- package/lib/data-structures/binary-tree/tree-multiset.js +50 -42
- 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 +46 -40
- package/src/data-structures/binary-tree/binary-tree.ts +328 -207
- package/src/data-structures/binary-tree/bst.ts +135 -88
- package/src/data-structures/binary-tree/tree-multiset.ts +50 -42
- 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
|
@@ -50,11 +50,11 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
50
50
|
return new TreeMultisetNode(key, val, count);
|
|
51
51
|
}
|
|
52
52
|
/**
|
|
53
|
-
* The function swaps the
|
|
54
|
-
* @param {N} srcNode - The source node that
|
|
55
|
-
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
56
|
-
* be swapped
|
|
57
|
-
* @returns the `destNode` after swapping its
|
|
53
|
+
* The function swaps the values of two nodes in a binary tree.
|
|
54
|
+
* @param {N} srcNode - The source node that needs to be swapped with the destination node.
|
|
55
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
56
|
+
* from `srcNode` will be swapped into.
|
|
57
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
58
58
|
*/
|
|
59
59
|
_swap(srcNode, destNode) {
|
|
60
60
|
const { key, val, count, height } = destNode;
|
|
@@ -73,14 +73,17 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
73
73
|
return destNode;
|
|
74
74
|
}
|
|
75
75
|
/**
|
|
76
|
-
* The `add` function adds a new node to a binary search tree,
|
|
77
|
-
* necessary.
|
|
78
|
-
* @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a
|
|
79
|
-
* represents a `
|
|
80
|
-
*
|
|
81
|
-
* @param
|
|
82
|
-
*
|
|
83
|
-
* @
|
|
76
|
+
* The `add` function adds a new node to a binary search tree, updating the count if the key already
|
|
77
|
+
* exists, and balancing the tree if necessary.
|
|
78
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
|
|
79
|
+
* `BinaryTreeNodeKey` (which represents the key of the node to be added), a `N` (which represents a
|
|
80
|
+
* node to be added), or `null` (which represents a null node).
|
|
81
|
+
* @param [val] - The `val` parameter represents the value associated with the key that is being
|
|
82
|
+
* added to the binary tree.
|
|
83
|
+
* @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
|
|
84
|
+
* pair that will be added to the binary tree. It has a default value of 1, which means that if no
|
|
85
|
+
* count is specified, the default count will be 1.
|
|
86
|
+
* @returns The function `add` returns a value of type `N | null | undefined`.
|
|
84
87
|
*/
|
|
85
88
|
add(keyOrNode, val, count = 1) {
|
|
86
89
|
let inserted = undefined, newNode;
|
|
@@ -159,13 +162,12 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
159
162
|
return inserted;
|
|
160
163
|
}
|
|
161
164
|
/**
|
|
162
|
-
* The function adds a new node to a binary tree if there is an available slot
|
|
163
|
-
* node
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
165
|
+
* The function adds a new node to a binary tree if there is an available slot in the parent node.
|
|
166
|
+
* @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
|
|
167
|
+
* the tree. It can be either a node object (`N`) or `null`.
|
|
168
|
+
* @param {N} parent - The `parent` parameter represents the parent node to which the new node will
|
|
169
|
+
* be added as a child.
|
|
170
|
+
* @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
|
|
169
171
|
*/
|
|
170
172
|
_addTo(newNode, parent) {
|
|
171
173
|
if (parent) {
|
|
@@ -194,13 +196,13 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
194
196
|
}
|
|
195
197
|
}
|
|
196
198
|
/**
|
|
197
|
-
* The `addMany` function
|
|
198
|
-
*
|
|
199
|
-
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of
|
|
200
|
-
*
|
|
201
|
-
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values
|
|
202
|
-
* the nodes being added. It is used
|
|
203
|
-
*
|
|
199
|
+
* The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
|
|
200
|
+
* inserted nodes.
|
|
201
|
+
* @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
|
|
202
|
+
* added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
|
|
203
|
+
* @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
|
|
204
|
+
* to the keys or nodes being added to the multiset. It is used to associate additional data with
|
|
205
|
+
* each key or node.
|
|
204
206
|
* @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
|
|
205
207
|
*/
|
|
206
208
|
addMany(keysOrNodes, data) {
|
|
@@ -220,16 +222,19 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
220
222
|
return inserted;
|
|
221
223
|
}
|
|
222
224
|
/**
|
|
223
|
-
* The `perfectlyBalance` function
|
|
224
|
-
*
|
|
225
|
-
* @
|
|
225
|
+
* The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
|
|
226
|
+
* binary search tree using either a recursive or iterative approach.
|
|
227
|
+
* @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
|
|
228
|
+
* type of iteration to use when building a balanced binary search tree. It can have two possible
|
|
229
|
+
* values:
|
|
230
|
+
* @returns a boolean value.
|
|
226
231
|
*/
|
|
227
|
-
perfectlyBalance() {
|
|
232
|
+
perfectlyBalance(iterationType = this.iterationType) {
|
|
228
233
|
const sorted = this.dfs(node => node, 'in'), n = sorted.length;
|
|
229
234
|
if (sorted.length < 1)
|
|
230
235
|
return false;
|
|
231
236
|
this.clear();
|
|
232
|
-
if (
|
|
237
|
+
if (iterationType === types_1.IterationType.RECURSIVE) {
|
|
233
238
|
const buildBalanceBST = (l, r) => {
|
|
234
239
|
if (l > r)
|
|
235
240
|
return;
|
|
@@ -261,13 +266,16 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
261
266
|
}
|
|
262
267
|
}
|
|
263
268
|
/**
|
|
264
|
-
* The `delete` function
|
|
265
|
-
* node that needs to be balanced.
|
|
266
|
-
* @param {N | BinaryTreeNodeKey
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
269
|
+
* The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
|
|
270
|
+
* node along with the parent node that needs to be balanced.
|
|
271
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
|
|
272
|
+
* (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
|
|
273
|
+
* from the binary tree.
|
|
274
|
+
* @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
|
|
275
|
+
* being deleted. If set to true, the count of the node will not be considered and the node will be
|
|
276
|
+
* deleted regardless of its count. If set to false (default), the count of the node will be
|
|
277
|
+
* decremented by 1 and
|
|
278
|
+
* @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
|
|
271
279
|
*/
|
|
272
280
|
delete(nodeOrKey, ignoreCount = false) {
|
|
273
281
|
const bstDeletedResult = [];
|
|
@@ -326,14 +334,14 @@ class TreeMultiset extends avl_tree_1.AVLTree {
|
|
|
326
334
|
return bstDeletedResult;
|
|
327
335
|
}
|
|
328
336
|
/**
|
|
329
|
-
* The clear() function clears the data and sets the count to
|
|
337
|
+
* The clear() function clears the contents of a data structure and sets the count to zero.
|
|
330
338
|
*/
|
|
331
339
|
clear() {
|
|
332
340
|
super.clear();
|
|
333
341
|
this._setCount(0);
|
|
334
342
|
}
|
|
335
343
|
/**
|
|
336
|
-
* The function
|
|
344
|
+
* The function sets the value of the "_count" property.
|
|
337
345
|
* @param {number} v - number
|
|
338
346
|
*/
|
|
339
347
|
_setCount(v) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tree-multiset.js","sourceRoot":"","sources":["../../../src/data-structures/binary-tree/tree-multiset.ts"],"names":[],"mappings":";;;AAQA,
|
|
1
|
+
{"version":3,"file":"tree-multiset.js","sourceRoot":"","sources":["../../../src/data-structures/binary-tree/tree-multiset.ts"],"names":[],"mappings":";;;AAQA,uCAAuF;AAEvF,yCAAgD;AAEhD,MAAa,gBAGX,SAAQ,sBAAsB;IAC9B;;;;;;;;;OASG;IACH,YAAY,GAAsB,EAAE,GAAO,EAAE,KAAK,GAAG,CAAC;QACpD,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAChB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CAGF;AApBD,4CAoBC;AAED;;GAEG;AACH,MAAa,YACX,SAAQ,kBAAU;IAGlB;;;;;OAKG;IACH,YAAY,OAA6B;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QAGT,WAAM,GAAG,CAAC,CAAC;IAFnB,CAAC;IAID,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;;;;;;;OAQG;IACM,UAAU,CAAC,GAAsB,EAAE,GAAc,EAAE,KAAc;QACxE,OAAO,IAAI,gBAAgB,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAM,CAAC;IACpD,CAAC;IAED;;;;;;OAMG;IACgB,KAAK,CAAC,OAAU,EAAE,QAAW;QAC9C,MAAM,EAAC,GAAG,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAC,GAAG,QAAQ,CAAC;QAC3C,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC;QAClD,IAAI,QAAQ,EAAE;YACZ,QAAQ,CAAC,MAAM,GAAG,MAAM,CAAC;YAEzB,QAAQ,CAAC,GAAG,GAAG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UAAU,CAAC,KAAK,CAAC,CAAC;SAC/C;QAED,gBAAgB,CAAC,IAAI,CAAC,EAAC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAC,CAAC,CAAC;QAE3D,IAAI,YAAY,EAAE;YAChB,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;SACjC;QAED,OAAO,gBAAgB,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,KAAK;QACH,KAAK,CAAC,KAAK,EAAE,CAAC;QACd,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;IAED;;;OAGG;IACO,SAAS,CAAC,CAAS;QAC3B,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IAClB,CAAC;CACF;AAjVD,oCAiVC"}
|
|
@@ -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
|
};
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FamilyPosition = exports.
|
|
3
|
+
exports.FamilyPosition = exports.IterationType = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Enum representing different loop types.
|
|
6
6
|
*
|
|
7
7
|
* - `iterative`: Indicates the iterative loop type (with loops that use iterations).
|
|
8
8
|
* - `recursive`: Indicates the recursive loop type (with loops that call themselves).
|
|
9
9
|
*/
|
|
10
|
-
var
|
|
11
|
-
(function (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
})(
|
|
10
|
+
var IterationType;
|
|
11
|
+
(function (IterationType) {
|
|
12
|
+
IterationType["ITERATIVE"] = "ITERATIVE";
|
|
13
|
+
IterationType["RECURSIVE"] = "RECURSIVE";
|
|
14
|
+
})(IterationType || (exports.IterationType = IterationType = {}));
|
|
15
15
|
var FamilyPosition;
|
|
16
16
|
(function (FamilyPosition) {
|
|
17
17
|
FamilyPosition["ROOT"] = "ROOT";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"binary-tree.js","sourceRoot":"","sources":["../../../src/types/data-structures/binary-tree.ts"],"names":[],"mappings":";;;AAEA;;;;;GAKG;AAEH,IAAY,
|
|
1
|
+
{"version":3,"file":"binary-tree.js","sourceRoot":"","sources":["../../../src/types/data-structures/binary-tree.ts"],"names":[],"mappings":";;;AAEA;;;;;GAKG;AAEH,IAAY,aAGX;AAHD,WAAY,aAAa;IACvB,wCAAuB,CAAA;IACvB,wCAAuB,CAAA;AACzB,CAAC,EAHW,aAAa,6BAAb,aAAa,QAGxB;AAED,IAAY,cAQX;AARD,WAAY,cAAc;IACxB,+BAAa,CAAA;IACb,+BAAa,CAAA;IACb,iCAAe,CAAA;IACf,yCAAuB,CAAA;IACvB,2CAAyB,CAAA;IACzB,uCAAqB,CAAA;IACrB,uCAAqB,CAAA;AACvB,CAAC,EARW,cAAc,8BAAd,cAAc,QAQzB"}
|
|
@@ -21,74 +21,80 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
|
|
|
21
21
|
*/
|
|
22
22
|
constructor(options?: AVLTreeOptions);
|
|
23
23
|
/**
|
|
24
|
-
* The
|
|
25
|
-
* @param {N} srcNode - The source node that
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
24
|
+
* The function swaps the key, value, and height properties between two nodes in a binary tree.
|
|
25
|
+
* @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
|
|
26
|
+
* with the `destNode`.
|
|
27
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
28
|
+
* from the source node (`srcNode`) will be swapped to.
|
|
29
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
29
30
|
*/
|
|
30
31
|
protected _swap(srcNode: N, destNode: N): N;
|
|
31
32
|
/**
|
|
32
|
-
* The function creates a new AVL tree node with the
|
|
33
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
34
|
-
*
|
|
35
|
-
* @param [val] - The `val`
|
|
36
|
-
*
|
|
33
|
+
* The function creates a new AVL tree node with the specified key and value.
|
|
34
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
35
|
+
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
36
|
+
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
|
|
37
|
+
* type `N['val']`, which means it can be any value that is assignable to the `val` property of the
|
|
38
|
+
* node type `N`.
|
|
37
39
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
38
40
|
*/
|
|
39
41
|
createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
|
|
40
42
|
/**
|
|
41
|
-
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
42
|
-
*
|
|
43
|
-
* @param
|
|
44
|
-
* `
|
|
45
|
-
* @
|
|
43
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
44
|
+
* a new node.
|
|
45
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
46
|
+
* `BinaryTreeNodeKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
47
|
+
* @param [val] - The `val` parameter is the value that you want to assign to the new node that you
|
|
48
|
+
* are adding to the binary search tree.
|
|
49
|
+
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
46
50
|
*/
|
|
47
|
-
add(
|
|
51
|
+
add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
|
|
48
52
|
/**
|
|
49
|
-
* The function overrides the delete method of a binary tree and
|
|
50
|
-
*
|
|
51
|
-
* @param {BinaryTreeNodeKey}
|
|
52
|
-
*
|
|
53
|
+
* The function overrides the delete method of a binary tree and balances the tree after deleting a
|
|
54
|
+
* node if necessary.
|
|
55
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
|
|
56
|
+
* (`N`) or a key value (`BinaryTreeNodeKey`).
|
|
53
57
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
54
58
|
*/
|
|
55
|
-
delete(
|
|
59
|
+
delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
|
|
56
60
|
/**
|
|
57
|
-
* The balance factor of a
|
|
58
|
-
*
|
|
59
|
-
* @
|
|
60
|
-
*
|
|
61
|
+
* The function calculates the balance factor of a node in a binary tree.
|
|
62
|
+
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
63
|
+
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
64
|
+
* height of the left subtree from the height of the right subtree.
|
|
61
65
|
*/
|
|
62
66
|
protected _balanceFactor(node: N): number;
|
|
63
67
|
/**
|
|
64
|
-
* The function updates the height of a node in
|
|
65
|
-
*
|
|
68
|
+
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
69
|
+
* right children.
|
|
70
|
+
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
66
71
|
*/
|
|
67
72
|
protected _updateHeight(node: N): void;
|
|
68
73
|
/**
|
|
69
|
-
* The `_balancePath` function
|
|
70
|
-
*
|
|
71
|
-
* @param node - The `node` parameter
|
|
74
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
75
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
76
|
+
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
77
|
+
* AVL tree that needs to be balanced.
|
|
72
78
|
*/
|
|
73
79
|
protected _balancePath(node: N): void;
|
|
74
80
|
/**
|
|
75
|
-
* The `_balanceLL`
|
|
76
|
-
* @param A -
|
|
81
|
+
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
82
|
+
* @param {N} A - A is a node in a binary tree.
|
|
77
83
|
*/
|
|
78
84
|
protected _balanceLL(A: N): void;
|
|
79
85
|
/**
|
|
80
|
-
* The `_balanceLR` function performs a left-right rotation to balance
|
|
81
|
-
* @param A - A is
|
|
86
|
+
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
87
|
+
* @param {N} A - A is a node in a binary tree.
|
|
82
88
|
*/
|
|
83
89
|
protected _balanceLR(A: N): void;
|
|
84
90
|
/**
|
|
85
|
-
* The `_balanceRR`
|
|
86
|
-
* @param A -
|
|
91
|
+
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
92
|
+
* @param {N} A - A is a node in a binary tree.
|
|
87
93
|
*/
|
|
88
94
|
protected _balanceRR(A: N): void;
|
|
89
95
|
/**
|
|
90
|
-
* The `_balanceRL`
|
|
91
|
-
* @param A - A is
|
|
96
|
+
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
97
|
+
* @param {N} A - A is a node in a binary tree.
|
|
92
98
|
*/
|
|
93
99
|
protected _balanceRL(A: N): void;
|
|
94
100
|
}
|
|
@@ -23,11 +23,12 @@ export class AVLTree extends BST {
|
|
|
23
23
|
super(options);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
* The
|
|
27
|
-
* @param {N} srcNode - The source node that
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
26
|
+
* The function swaps the key, value, and height properties between two nodes in a binary tree.
|
|
27
|
+
* @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
|
|
28
|
+
* with the `destNode`.
|
|
29
|
+
* @param {N} destNode - The `destNode` parameter represents the destination node where the values
|
|
30
|
+
* from the source node (`srcNode`) will be swapped to.
|
|
31
|
+
* @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
|
|
31
32
|
*/
|
|
32
33
|
_swap(srcNode, destNode) {
|
|
33
34
|
const { key, val, height } = destNode;
|
|
@@ -44,39 +45,42 @@ export class AVLTree extends BST {
|
|
|
44
45
|
return destNode;
|
|
45
46
|
}
|
|
46
47
|
/**
|
|
47
|
-
* The function creates a new AVL tree node with the
|
|
48
|
-
* @param {BinaryTreeNodeKey} key - The
|
|
49
|
-
*
|
|
50
|
-
* @param [val] - The `val`
|
|
51
|
-
*
|
|
48
|
+
* The function creates a new AVL tree node with the specified key and value.
|
|
49
|
+
* @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
|
|
50
|
+
* the new node. It is used to determine the position of the node in the binary search tree.
|
|
51
|
+
* @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
|
|
52
|
+
* type `N['val']`, which means it can be any value that is assignable to the `val` property of the
|
|
53
|
+
* node type `N`.
|
|
52
54
|
* @returns a new AVLTreeNode object with the specified key and value.
|
|
53
55
|
*/
|
|
54
56
|
createNode(key, val) {
|
|
55
57
|
return new AVLTreeNode(key, val);
|
|
56
58
|
}
|
|
57
59
|
/**
|
|
58
|
-
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
59
|
-
*
|
|
60
|
-
* @param
|
|
61
|
-
* `
|
|
62
|
-
* @
|
|
60
|
+
* The function overrides the add method of a binary tree node and balances the tree after inserting
|
|
61
|
+
* a new node.
|
|
62
|
+
* @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
|
|
63
|
+
* `BinaryTreeNodeKey` or a `N` (which represents a node in the binary tree) or `null`.
|
|
64
|
+
* @param [val] - The `val` parameter is the value that you want to assign to the new node that you
|
|
65
|
+
* are adding to the binary search tree.
|
|
66
|
+
* @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
|
|
63
67
|
*/
|
|
64
|
-
add(
|
|
68
|
+
add(keyOrNode, val) {
|
|
65
69
|
// TODO support node as a param
|
|
66
|
-
const inserted = super.add(
|
|
70
|
+
const inserted = super.add(keyOrNode, val);
|
|
67
71
|
if (inserted)
|
|
68
72
|
this._balancePath(inserted);
|
|
69
73
|
return inserted;
|
|
70
74
|
}
|
|
71
75
|
/**
|
|
72
|
-
* The function overrides the delete method of a binary tree and
|
|
73
|
-
*
|
|
74
|
-
* @param {BinaryTreeNodeKey}
|
|
75
|
-
*
|
|
76
|
+
* The function overrides the delete method of a binary tree and balances the tree after deleting a
|
|
77
|
+
* node if necessary.
|
|
78
|
+
* @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
|
|
79
|
+
* (`N`) or a key value (`BinaryTreeNodeKey`).
|
|
76
80
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
77
81
|
*/
|
|
78
|
-
delete(
|
|
79
|
-
const deletedResults = super.delete(
|
|
82
|
+
delete(nodeOrKey) {
|
|
83
|
+
const deletedResults = super.delete(nodeOrKey);
|
|
80
84
|
for (const { needBalanced } of deletedResults) {
|
|
81
85
|
if (needBalanced) {
|
|
82
86
|
this._balancePath(needBalanced);
|
|
@@ -85,10 +89,10 @@ export class AVLTree extends BST {
|
|
|
85
89
|
return deletedResults;
|
|
86
90
|
}
|
|
87
91
|
/**
|
|
88
|
-
* The balance factor of a
|
|
89
|
-
*
|
|
90
|
-
* @
|
|
91
|
-
*
|
|
92
|
+
* The function calculates the balance factor of a node in a binary tree.
|
|
93
|
+
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
94
|
+
* @returns the balance factor of a given node. The balance factor is calculated by subtracting the
|
|
95
|
+
* height of the left subtree from the height of the right subtree.
|
|
92
96
|
*/
|
|
93
97
|
_balanceFactor(node) {
|
|
94
98
|
if (!node.right)
|
|
@@ -101,8 +105,9 @@ export class AVLTree extends BST {
|
|
|
101
105
|
return node.right.height - node.left.height;
|
|
102
106
|
}
|
|
103
107
|
/**
|
|
104
|
-
* The function updates the height of a node in
|
|
105
|
-
*
|
|
108
|
+
* The function updates the height of a node in a binary tree based on the heights of its left and
|
|
109
|
+
* right children.
|
|
110
|
+
* @param {N} node - The parameter "node" represents a node in a binary tree data structure.
|
|
106
111
|
*/
|
|
107
112
|
_updateHeight(node) {
|
|
108
113
|
if (!node.left && !node.right)
|
|
@@ -117,9 +122,10 @@ export class AVLTree extends BST {
|
|
|
117
122
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
118
123
|
}
|
|
119
124
|
/**
|
|
120
|
-
* The `_balancePath` function
|
|
121
|
-
*
|
|
122
|
-
* @param node - The `node` parameter
|
|
125
|
+
* The `_balancePath` function is used to update the heights of nodes and perform rotation operations
|
|
126
|
+
* to restore balance in an AVL tree after inserting a node.
|
|
127
|
+
* @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
|
|
128
|
+
* AVL tree that needs to be balanced.
|
|
123
129
|
*/
|
|
124
130
|
_balancePath(node) {
|
|
125
131
|
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
@@ -161,8 +167,8 @@ export class AVLTree extends BST {
|
|
|
161
167
|
}
|
|
162
168
|
}
|
|
163
169
|
/**
|
|
164
|
-
* The `_balanceLL`
|
|
165
|
-
* @param A -
|
|
170
|
+
* The function `_balanceLL` performs a left-left rotation to balance a binary tree.
|
|
171
|
+
* @param {N} A - A is a node in a binary tree.
|
|
166
172
|
*/
|
|
167
173
|
_balanceLL(A) {
|
|
168
174
|
const parentOfA = A.parent;
|
|
@@ -195,8 +201,8 @@ export class AVLTree extends BST {
|
|
|
195
201
|
this._updateHeight(B);
|
|
196
202
|
}
|
|
197
203
|
/**
|
|
198
|
-
* The `_balanceLR` function performs a left-right rotation to balance
|
|
199
|
-
* @param A - A is
|
|
204
|
+
* The `_balanceLR` function performs a left-right rotation to balance a binary tree.
|
|
205
|
+
* @param {N} A - A is a node in a binary tree.
|
|
200
206
|
*/
|
|
201
207
|
_balanceLR(A) {
|
|
202
208
|
const parentOfA = A.parent;
|
|
@@ -244,8 +250,8 @@ export class AVLTree extends BST {
|
|
|
244
250
|
C && this._updateHeight(C);
|
|
245
251
|
}
|
|
246
252
|
/**
|
|
247
|
-
* The `_balanceRR`
|
|
248
|
-
* @param A -
|
|
253
|
+
* The function `_balanceRR` performs a right-right rotation to balance a binary tree.
|
|
254
|
+
* @param {N} A - A is a node in a binary tree.
|
|
249
255
|
*/
|
|
250
256
|
_balanceRR(A) {
|
|
251
257
|
const parentOfA = A.parent;
|
|
@@ -279,8 +285,8 @@ export class AVLTree extends BST {
|
|
|
279
285
|
B && this._updateHeight(B);
|
|
280
286
|
}
|
|
281
287
|
/**
|
|
282
|
-
* The `_balanceRL`
|
|
283
|
-
* @param A - A is
|
|
288
|
+
* The function `_balanceRL` performs a right-left rotation to balance a binary tree.
|
|
289
|
+
* @param {N} A - A is a node in a binary tree.
|
|
284
290
|
*/
|
|
285
291
|
_balanceRL(A) {
|
|
286
292
|
const parentOfA = A.parent;
|