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.
Files changed (28) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +42 -34
  3. package/dist/data-structures/binary-tree/avl-tree.js +42 -34
  4. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +265 -168
  6. package/dist/data-structures/binary-tree/binary-tree.js +257 -170
  7. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/bst.d.ts +104 -59
  9. package/dist/data-structures/binary-tree/bst.js +105 -60
  10. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +47 -39
  12. package/dist/data-structures/binary-tree/tree-multiset.js +47 -39
  13. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  14. package/lib/data-structures/binary-tree/avl-tree.d.ts +42 -34
  15. package/lib/data-structures/binary-tree/avl-tree.js +42 -34
  16. package/lib/data-structures/binary-tree/binary-tree.d.ts +265 -168
  17. package/lib/data-structures/binary-tree/binary-tree.js +257 -170
  18. package/lib/data-structures/binary-tree/bst.d.ts +104 -59
  19. package/lib/data-structures/binary-tree/bst.js +105 -60
  20. package/lib/data-structures/binary-tree/tree-multiset.d.ts +47 -39
  21. package/lib/data-structures/binary-tree/tree-multiset.js +47 -39
  22. package/package.json +5 -5
  23. package/src/data-structures/binary-tree/avl-tree.ts +42 -34
  24. package/src/data-structures/binary-tree/binary-tree.ts +270 -174
  25. package/src/data-structures/binary-tree/bst.ts +108 -66
  26. package/src/data-structures/binary-tree/tree-multiset.ts +47 -39
  27. package/umd/bundle.min.js +1 -1
  28. package/umd/bundle.min.js.map +1 -1
@@ -46,11 +46,11 @@ export class TreeMultiset extends AVLTree {
46
46
  return new TreeMultisetNode(key, val, count);
47
47
  }
48
48
  /**
49
- * The function swaps the location of two nodes in a tree data structure.
50
- * @param {N} srcNode - The source node that we want to _swap with the destination node.
51
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
52
- * be swapped with.
53
- * @returns the `destNode` after swapping its values with the `srcNode`.
49
+ * The function swaps the values of two nodes in a binary tree.
50
+ * @param {N} srcNode - The source node that needs to be swapped with the destination node.
51
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
52
+ * from `srcNode` will be swapped into.
53
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
54
54
  */
55
55
  _swap(srcNode, destNode) {
56
56
  const { key, val, count, height } = destNode;
@@ -69,14 +69,17 @@ export class TreeMultiset extends AVLTree {
69
69
  return destNode;
70
70
  }
71
71
  /**
72
- * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
73
- * necessary.
74
- * @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
75
- * represents a `BinaryTreeNode`).
76
- * @param [val] - The `val` parameter represents the value to be added to the binary tree node.
77
- * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
78
- * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
79
- * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
72
+ * The `add` function adds a new node to a binary search tree, updating the count if the key already
73
+ * exists, and balancing the tree if necessary.
74
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
75
+ * `BinaryTreeNodeKey` (which represents the key of the node to be added), a `N` (which represents a
76
+ * node to be added), or `null` (which represents a null node).
77
+ * @param [val] - The `val` parameter represents the value associated with the key that is being
78
+ * added to the binary tree.
79
+ * @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
80
+ * pair that will be added to the binary tree. It has a default value of 1, which means that if no
81
+ * count is specified, the default count will be 1.
82
+ * @returns The function `add` returns a value of type `N | null | undefined`.
80
83
  */
81
84
  add(keyOrNode, val, count = 1) {
82
85
  let inserted = undefined, newNode;
@@ -155,13 +158,12 @@ export class TreeMultiset extends AVLTree {
155
158
  return inserted;
156
159
  }
157
160
  /**
158
- * The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent
159
- * node.
160
- * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
161
- * be either a node object (`N`) or `null`.
162
- * @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
163
- * child.
164
- * @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
161
+ * The function adds a new node to a binary tree if there is an available slot in the parent node.
162
+ * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
163
+ * the tree. It can be either a node object (`N`) or `null`.
164
+ * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
165
+ * be added as a child.
166
+ * @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
165
167
  */
166
168
  _addTo(newNode, parent) {
167
169
  if (parent) {
@@ -190,13 +192,13 @@ export class TreeMultiset extends AVLTree {
190
192
  }
191
193
  }
192
194
  /**
193
- * The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
194
- * the inserted nodes.
195
- * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
196
- * objects, or null values.
197
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
198
- * the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
199
- * method. If provided, the `data` array should
195
+ * The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
196
+ * inserted nodes.
197
+ * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
198
+ * added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
199
+ * @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
200
+ * to the keys or nodes being added to the multiset. It is used to associate additional data with
201
+ * each key or node.
200
202
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
201
203
  */
202
204
  addMany(keysOrNodes, data) {
@@ -216,9 +218,12 @@ export class TreeMultiset extends AVLTree {
216
218
  return inserted;
217
219
  }
218
220
  /**
219
- * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
220
- * constructs a balanced binary search tree using either a recursive or iterative approach.
221
- * @returns The function `perfectlyBalance()` returns a boolean value.
221
+ * The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
222
+ * binary search tree using either a recursive or iterative approach.
223
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
224
+ * type of iteration to use when building a balanced binary search tree. It can have two possible
225
+ * values:
226
+ * @returns a boolean value.
222
227
  */
223
228
  perfectlyBalance(iterationType = this.iterationType) {
224
229
  const sorted = this.dfs(node => node, 'in'), n = sorted.length;
@@ -257,13 +262,16 @@ export class TreeMultiset extends AVLTree {
257
262
  }
258
263
  }
259
264
  /**
260
- * The `delete` function removes a node from a binary search tree and returns the deleted node along with the parent
261
- * node that needs to be balanced.
262
- * @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
263
- * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
264
- * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
265
- * not be taken into account when removing it. If `ignoreCount` is set to `false
266
- * @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
265
+ * The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
266
+ * node along with the parent node that needs to be balanced.
267
+ * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
268
+ * (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
269
+ * from the binary tree.
270
+ * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
271
+ * being deleted. If set to true, the count of the node will not be considered and the node will be
272
+ * deleted regardless of its count. If set to false (default), the count of the node will be
273
+ * decremented by 1 and
274
+ * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
267
275
  */
268
276
  delete(nodeOrKey, ignoreCount = false) {
269
277
  const bstDeletedResult = [];
@@ -322,14 +330,14 @@ export class TreeMultiset extends AVLTree {
322
330
  return bstDeletedResult;
323
331
  }
324
332
  /**
325
- * The clear() function clears the data and sets the count to 0.
333
+ * The clear() function clears the contents of a data structure and sets the count to zero.
326
334
  */
327
335
  clear() {
328
336
  super.clear();
329
337
  this._setCount(0);
330
338
  }
331
339
  /**
332
- * The function "_setCount" is used to set the value of the "_count" property.
340
+ * The function sets the value of the "_count" property.
333
341
  * @param {number} v - number
334
342
  */
335
343
  _setCount(v) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.37.3",
3
+ "version": "1.37.4",
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",
@@ -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.2",
61
+ "avl-tree-typed": "^1.37.3",
62
62
  "benchmark": "^2.1.4",
63
- "binary-tree-typed": "^1.37.2",
64
- "bst-typed": "^1.37.2",
63
+ "binary-tree-typed": "^1.37.3",
64
+ "bst-typed": "^1.37.3",
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.2",
71
+ "heap-typed": "^1.37.3",
72
72
  "istanbul-badges-readme": "^1.8.5",
73
73
  "jest": "^29.7.0",
74
74
  "prettier": "^3.0.3",
@@ -33,11 +33,12 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
33
33
  }
34
34
 
35
35
  /**
36
- * The `_swap` function swaps the location of two nodes in a binary tree.
37
- * @param {N} srcNode - The source node that you want to _swap with the destination node.
38
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
39
- * be swapped to.
40
- * @returns The `destNode` is being returned.
36
+ * The function swaps the key, value, and height properties between two nodes in a binary tree.
37
+ * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
38
+ * with the `destNode`.
39
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
40
+ * from the source node (`srcNode`) will be swapped to.
41
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
41
42
  */
42
43
  protected override _swap(srcNode: N, destNode: N): N {
43
44
  const {key, val, height} = destNode;
@@ -59,11 +60,12 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
59
60
  }
60
61
 
61
62
  /**
62
- * The function creates a new AVL tree node with the given key and value.
63
- * @param {BinaryTreeNodeKey} key - The `key` parameter is the identifier for the binary tree node. It is used to uniquely
64
- * identify each node in the 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.
63
+ * The function creates a new AVL tree node with the specified key and value.
64
+ * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
65
+ * the new node. It is used to determine the position of the node in the binary search tree.
66
+ * @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
67
+ * type `N['val']`, which means it can be any value that is assignable to the `val` property of the
68
+ * node type `N`.
67
69
  * @returns a new AVLTreeNode object with the specified key and value.
68
70
  */
69
71
  override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
@@ -71,11 +73,13 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
71
73
  }
72
74
 
73
75
  /**
74
- * The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
75
- * @param keyOrNode - The `keyOrNode` parameter is either a key or a node that needs to be added to the binary tree.
76
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
77
- * `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
78
- * @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
76
+ * The function overrides the add method of a binary tree node and balances the tree after inserting
77
+ * a new node.
78
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can accept either a
79
+ * `BinaryTreeNodeKey` or a `N` (which represents a node in the binary tree) or `null`.
80
+ * @param [val] - The `val` parameter is the value that you want to assign to the new node that you
81
+ * are adding to the binary search tree.
82
+ * @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
79
83
  */
80
84
  override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
81
85
  // TODO support node as a param
@@ -85,9 +89,11 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
85
89
  }
86
90
 
87
91
  /**
88
- * The function overrides the delete method of a binary tree and performs additional operations to balance the tree after deletion.
92
+ * The function overrides the delete method of a binary tree and balances the tree after deleting a
93
+ * node if necessary.
94
+ * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
95
+ * (`N`) or a key value (`BinaryTreeNodeKey`).
89
96
  * @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.
91
97
  */
92
98
  override delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
93
99
  const deletedResults = super.delete(nodeOrKey);
@@ -100,10 +106,10 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
100
106
  }
101
107
 
102
108
  /**
103
- * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
104
- * height of its right subtree.
105
- * @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
106
- * @returns The balance factor of the given AVL tree node.
109
+ * The function calculates the balance factor of a node in a binary tree.
110
+ * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
111
+ * @returns the balance factor of a given node. The balance factor is calculated by subtracting the
112
+ * height of the left subtree from the height of the right subtree.
107
113
  */
108
114
  protected _balanceFactor(node: N): number {
109
115
  if (!node.right)
@@ -116,8 +122,9 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
116
122
  }
117
123
 
118
124
  /**
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.
125
+ * The function updates the height of a node in a binary tree based on the heights of its left and
126
+ * right children.
127
+ * @param {N} node - The parameter "node" represents a node in a binary tree data structure.
121
128
  */
122
129
  protected _updateHeight(node: N): void {
123
130
  if (!node.left && !node.right) node.height = 0;
@@ -129,9 +136,10 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
129
136
  }
130
137
 
131
138
  /**
132
- * The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
133
- * each node in the path from the given node to the root.
134
- * @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
139
+ * The `_balancePath` function is used to update the heights of nodes and perform rotation operations
140
+ * to restore balance in an AVL tree after inserting a node.
141
+ * @param {N} node - The `node` parameter in the `_balancePath` function represents the node in the
142
+ * AVL tree that needs to be balanced.
135
143
  */
136
144
  protected _balancePath(node: N): void {
137
145
  const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
@@ -173,8 +181,8 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
173
181
  }
174
182
 
175
183
  /**
176
- * The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
177
- * @param A - The parameter A is an AVLTreeNode object.
184
+ * The function `_balanceLL` performs a left-left rotation to balance a binary tree.
185
+ * @param {N} A - A is a node in a binary tree.
178
186
  */
179
187
  protected _balanceLL(A: N): void {
180
188
  const parentOfA = A.parent;
@@ -203,8 +211,8 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
203
211
  }
204
212
 
205
213
  /**
206
- * The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
207
- * @param A - A is an AVLTreeNode object.
214
+ * The `_balanceLR` function performs a left-right rotation to balance a binary tree.
215
+ * @param {N} A - A is a node in a binary tree.
208
216
  */
209
217
  protected _balanceLR(A: N): void {
210
218
  const parentOfA = A.parent;
@@ -251,8 +259,8 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
251
259
  }
252
260
 
253
261
  /**
254
- * The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
255
- * @param A - The parameter A is an AVLTreeNode object.
262
+ * The function `_balanceRR` performs a right-right rotation to balance a binary tree.
263
+ * @param {N} A - A is a node in a binary tree.
256
264
  */
257
265
  protected _balanceRR(A: N): void {
258
266
  const parentOfA = A.parent;
@@ -286,8 +294,8 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
286
294
  }
287
295
 
288
296
  /**
289
- * The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
290
- * @param A - A is an AVLTreeNode object.
297
+ * The function `_balanceRL` performs a right-left rotation to balance a binary tree.
298
+ * @param {N} A - A is a node in a binary tree.
291
299
  */
292
300
  protected _balanceRL(A: N): void {
293
301
  const parentOfA = A.parent;