data-structure-typed 1.37.3 → 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 +1 -1
- package/dist/data-structures/binary-tree/avl-tree.d.ts +42 -34
- package/dist/data-structures/binary-tree/avl-tree.js +42 -34
- package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/binary-tree.d.ts +265 -168
- package/dist/data-structures/binary-tree/binary-tree.js +257 -170
- package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
- package/dist/data-structures/binary-tree/bst.d.ts +104 -59
- package/dist/data-structures/binary-tree/bst.js +105 -60
- package/dist/data-structures/binary-tree/bst.js.map +1 -1
- package/dist/data-structures/binary-tree/tree-multiset.d.ts +47 -39
- package/dist/data-structures/binary-tree/tree-multiset.js +47 -39
- package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
- package/lib/data-structures/binary-tree/avl-tree.d.ts +42 -34
- package/lib/data-structures/binary-tree/avl-tree.js +42 -34
- package/lib/data-structures/binary-tree/binary-tree.d.ts +265 -168
- package/lib/data-structures/binary-tree/binary-tree.js +257 -170
- package/lib/data-structures/binary-tree/bst.d.ts +104 -59
- package/lib/data-structures/binary-tree/bst.js +105 -60
- package/lib/data-structures/binary-tree/tree-multiset.d.ts +47 -39
- package/lib/data-structures/binary-tree/tree-multiset.js +47 -39
- package/package.json +5 -5
- package/src/data-structures/binary-tree/avl-tree.ts +42 -34
- package/src/data-structures/binary-tree/binary-tree.ts +270 -174
- package/src/data-structures/binary-tree/bst.ts +108 -66
- package/src/data-structures/binary-tree/tree-multiset.ts +47 -39
- package/umd/bundle.min.js +1 -1
- package/umd/bundle.min.js.map +1 -1
|
@@ -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,22 +45,25 @@ 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
68
|
add(keyOrNode, val) {
|
|
65
69
|
// TODO support node as a param
|
|
@@ -69,9 +73,11 @@ export class AVLTree extends BST {
|
|
|
69
73
|
return inserted;
|
|
70
74
|
}
|
|
71
75
|
/**
|
|
72
|
-
* The function overrides the delete method of a binary tree and
|
|
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`).
|
|
73
80
|
* @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
|
|
74
|
-
* @param nodeOrKey - The `nodeOrKey` parameter is either a node or a key that needs to be deleted from the binary tree.
|
|
75
81
|
*/
|
|
76
82
|
delete(nodeOrKey) {
|
|
77
83
|
const deletedResults = super.delete(nodeOrKey);
|
|
@@ -83,10 +89,10 @@ export class AVLTree extends BST {
|
|
|
83
89
|
return deletedResults;
|
|
84
90
|
}
|
|
85
91
|
/**
|
|
86
|
-
* The balance factor of a
|
|
87
|
-
*
|
|
88
|
-
* @
|
|
89
|
-
*
|
|
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.
|
|
90
96
|
*/
|
|
91
97
|
_balanceFactor(node) {
|
|
92
98
|
if (!node.right)
|
|
@@ -99,8 +105,9 @@ export class AVLTree extends BST {
|
|
|
99
105
|
return node.right.height - node.left.height;
|
|
100
106
|
}
|
|
101
107
|
/**
|
|
102
|
-
* The function updates the height of a node in
|
|
103
|
-
*
|
|
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.
|
|
104
111
|
*/
|
|
105
112
|
_updateHeight(node) {
|
|
106
113
|
if (!node.left && !node.right)
|
|
@@ -115,9 +122,10 @@ export class AVLTree extends BST {
|
|
|
115
122
|
node.height = 1 + Math.max(node.right.height, node.left.height);
|
|
116
123
|
}
|
|
117
124
|
/**
|
|
118
|
-
* The `_balancePath` function
|
|
119
|
-
*
|
|
120
|
-
* @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.
|
|
121
129
|
*/
|
|
122
130
|
_balancePath(node) {
|
|
123
131
|
const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
|
|
@@ -159,8 +167,8 @@ export class AVLTree extends BST {
|
|
|
159
167
|
}
|
|
160
168
|
}
|
|
161
169
|
/**
|
|
162
|
-
* The `_balanceLL`
|
|
163
|
-
* @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.
|
|
164
172
|
*/
|
|
165
173
|
_balanceLL(A) {
|
|
166
174
|
const parentOfA = A.parent;
|
|
@@ -193,8 +201,8 @@ export class AVLTree extends BST {
|
|
|
193
201
|
this._updateHeight(B);
|
|
194
202
|
}
|
|
195
203
|
/**
|
|
196
|
-
* The `_balanceLR` function performs a left-right rotation to balance
|
|
197
|
-
* @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.
|
|
198
206
|
*/
|
|
199
207
|
_balanceLR(A) {
|
|
200
208
|
const parentOfA = A.parent;
|
|
@@ -242,8 +250,8 @@ export class AVLTree extends BST {
|
|
|
242
250
|
C && this._updateHeight(C);
|
|
243
251
|
}
|
|
244
252
|
/**
|
|
245
|
-
* The `_balanceRR`
|
|
246
|
-
* @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.
|
|
247
255
|
*/
|
|
248
256
|
_balanceRR(A) {
|
|
249
257
|
const parentOfA = A.parent;
|
|
@@ -277,8 +285,8 @@ export class AVLTree extends BST {
|
|
|
277
285
|
B && this._updateHeight(B);
|
|
278
286
|
}
|
|
279
287
|
/**
|
|
280
|
-
* The `_balanceRL`
|
|
281
|
-
* @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.
|
|
282
290
|
*/
|
|
283
291
|
_balanceRL(A) {
|
|
284
292
|
const parentOfA = A.parent;
|