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.
Files changed (42) hide show
  1. package/CHANGELOG.md +3 -1
  2. package/dist/data-structures/binary-tree/avl-tree.d.ts +44 -38
  3. package/dist/data-structures/binary-tree/avl-tree.js +46 -40
  4. package/dist/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/data-structures/binary-tree/binary-tree.d.ts +305 -192
  6. package/dist/data-structures/binary-tree/binary-tree.js +304 -201
  7. package/dist/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/data-structures/binary-tree/bst.d.ts +111 -64
  9. package/dist/data-structures/binary-tree/bst.js +132 -85
  10. package/dist/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/data-structures/binary-tree/tree-multiset.d.ts +49 -41
  12. package/dist/data-structures/binary-tree/tree-multiset.js +49 -41
  13. package/dist/data-structures/binary-tree/tree-multiset.js.map +1 -1
  14. package/dist/types/data-structures/binary-tree.d.ts +2 -2
  15. package/dist/types/data-structures/binary-tree.js +6 -6
  16. package/dist/types/data-structures/binary-tree.js.map +1 -1
  17. package/lib/data-structures/binary-tree/avl-tree.d.ts +44 -38
  18. package/lib/data-structures/binary-tree/avl-tree.js +46 -40
  19. package/lib/data-structures/binary-tree/binary-tree.d.ts +305 -192
  20. package/lib/data-structures/binary-tree/binary-tree.js +305 -202
  21. package/lib/data-structures/binary-tree/bst.d.ts +111 -64
  22. package/lib/data-structures/binary-tree/bst.js +133 -86
  23. package/lib/data-structures/binary-tree/tree-multiset.d.ts +49 -41
  24. package/lib/data-structures/binary-tree/tree-multiset.js +50 -42
  25. package/lib/types/data-structures/binary-tree.d.ts +2 -2
  26. package/lib/types/data-structures/binary-tree.js +5 -5
  27. package/package.json +6 -6
  28. package/src/data-structures/binary-tree/avl-tree.ts +46 -40
  29. package/src/data-structures/binary-tree/binary-tree.ts +328 -207
  30. package/src/data-structures/binary-tree/bst.ts +135 -88
  31. package/src/data-structures/binary-tree/tree-multiset.ts +50 -42
  32. package/src/types/data-structures/binary-tree.ts +2 -2
  33. package/test/config.ts +1 -0
  34. package/test/integration/avl-tree.test.ts +7 -8
  35. package/test/integration/bst.test.ts +17 -16
  36. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +50 -0
  37. package/test/unit/data-structures/binary-tree/bst.test.ts +8 -1
  38. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -1
  39. package/test/unit/data-structures/linked-list/linked-list.test.ts +1 -1
  40. package/test/utils/big-o.ts +2 -1
  41. package/umd/bundle.min.js +1 -1
  42. package/umd/bundle.min.js.map +1 -1
@@ -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> {
@@ -47,67 +47,75 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
47
47
  */
48
48
  createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
49
49
  /**
50
- * The function swaps the location of two nodes in a tree data structure.
51
- * @param {N} srcNode - The source node that we want to _swap with the destination node.
52
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
53
- * be swapped with.
54
- * @returns the `destNode` after swapping its values with the `srcNode`.
50
+ * The function swaps the values of two nodes in a binary tree.
51
+ * @param {N} srcNode - The source node that needs to be swapped with the destination node.
52
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values
53
+ * from `srcNode` will be swapped into.
54
+ * @returns The method is returning the `destNode` after swapping its properties with the `srcNode`.
55
55
  */
56
56
  protected _swap(srcNode: N, destNode: N): N;
57
57
  /**
58
- * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
59
- * necessary.
60
- * @param {BinaryTreeNodeKey | N} keyOrNode - The `keyOrNode` parameter can be either a `BinaryTreeNodeKey` or a `N` (which
61
- * represents a `BinaryTreeNode`).
62
- * @param [val] - The `val` parameter represents the value to be added to the binary tree node.
63
- * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
64
- * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
65
- * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
58
+ * The `add` function adds a new node to a binary search tree, updating the count if the key already
59
+ * exists, and balancing the tree if necessary.
60
+ * @param {BinaryTreeNodeKey | N | null} keyOrNode - The `keyOrNode` parameter can be either a
61
+ * `BinaryTreeNodeKey` (which represents the key of the node to be added), a `N` (which represents a
62
+ * node to be added), or `null` (which represents a null node).
63
+ * @param [val] - The `val` parameter represents the value associated with the key that is being
64
+ * added to the binary tree.
65
+ * @param [count=1] - The `count` parameter represents the number of occurrences of the key/value
66
+ * pair that will be added to the binary tree. It has a default value of 1, which means that if no
67
+ * count is specified, the default count will be 1.
68
+ * @returns The function `add` returns a value of type `N | null | undefined`.
66
69
  */
67
70
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined;
68
71
  /**
69
- * 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
70
- * node.
71
- * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
72
- * be either a node object (`N`) or `null`.
73
- * @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
74
- * child.
75
- * @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
72
+ * The function adds a new node to a binary tree if there is an available slot in the parent node.
73
+ * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
74
+ * the tree. It can be either a node object (`N`) or `null`.
75
+ * @param {N} parent - The `parent` parameter represents the parent node to which the new node will
76
+ * be added as a child.
77
+ * @returns The method `_addTo` returns either the `parent.left`, `parent.right`, or `undefined`.
76
78
  */
77
79
  _addTo(newNode: N | null, parent: N): N | null | undefined;
78
80
  /**
79
- * The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
80
- * the inserted nodes.
81
- * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
82
- * objects, or null values.
83
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
84
- * the nodes being added. It is used when adding nodes using the `keyOrNode` and `data` arguments in the `this.add()`
85
- * method. If provided, the `data` array should
81
+ * The `addMany` function adds multiple keys or nodes to a TreeMultiset and returns an array of the
82
+ * inserted nodes.
83
+ * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
84
+ * added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
85
+ * @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
86
+ * to the keys or nodes being added to the multiset. It is used to associate additional data with
87
+ * each key or node.
86
88
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
87
89
  */
88
90
  addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[];
89
91
  /**
90
- * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
91
- * constructs a balanced binary search tree using either a recursive or iterative approach.
92
- * @returns The function `perfectlyBalance()` returns a boolean value.
92
+ * The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
93
+ * binary search tree using either a recursive or iterative approach.
94
+ * @param iterationType - The `iterationType` parameter is an optional parameter that specifies the
95
+ * type of iteration to use when building a balanced binary search tree. It can have two possible
96
+ * values:
97
+ * @returns a boolean value.
93
98
  */
94
- perfectlyBalance(): boolean;
99
+ perfectlyBalance(iterationType?: IterationType): boolean;
95
100
  /**
96
- * The `delete` function removes a node from a binary search tree and returns the deleted node along with the parent
97
- * node that needs to be balanced.
98
- * @param {N | BinaryTreeNodeKey | null} nodeOrKey - The `nodeOrKey` parameter can be one of the following:
99
- * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
100
- * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
101
- * not be taken into account when removing it. If `ignoreCount` is set to `false
102
- * @returns The function `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
101
+ * The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
102
+ * node along with the parent node that needs to be balanced.
103
+ * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
104
+ * (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
105
+ * from the binary tree.
106
+ * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
107
+ * being deleted. If set to true, the count of the node will not be considered and the node will be
108
+ * deleted regardless of its count. If set to false (default), the count of the node will be
109
+ * decremented by 1 and
110
+ * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
103
111
  */
104
112
  delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
105
113
  /**
106
- * The clear() function clears the data and sets the count to 0.
114
+ * The clear() function clears the contents of a data structure and sets the count to zero.
107
115
  */
108
116
  clear(): void;
109
117
  /**
110
- * The function "_setCount" is used to set the value of the "_count" property.
118
+ * The function sets the value of the "_count" property.
111
119
  * @param {number} v - number
112
120
  */
113
121
  protected _setCount(v: number): void;
@@ -1,4 +1,4 @@
1
- import { CP, FamilyPosition, LoopType } from '../../types';
1
+ import { CP, FamilyPosition, IterationType } from '../../types';
2
2
  import { AVLTree, AVLTreeNode } from './avl-tree';
3
3
  export class TreeMultisetNode extends AVLTreeNode {
4
4
  /**
@@ -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,16 +218,19 @@ 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
- perfectlyBalance() {
228
+ perfectlyBalance(iterationType = this.iterationType) {
224
229
  const sorted = this.dfs(node => node, 'in'), n = sorted.length;
225
230
  if (sorted.length < 1)
226
231
  return false;
227
232
  this.clear();
228
- if (this.loopType === LoopType.RECURSIVE) {
233
+ if (iterationType === IterationType.RECURSIVE) {
229
234
  const buildBalanceBST = (l, r) => {
230
235
  if (l > r)
231
236
  return;
@@ -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) {
@@ -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 LoopType {
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
- loopType?: LoopType;
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 LoopType;
8
- (function (LoopType) {
9
- LoopType["ITERATIVE"] = "ITERATIVE";
10
- LoopType["RECURSIVE"] = "RECURSIVE";
11
- })(LoopType || (LoopType = {}));
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.2",
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",
@@ -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.0",
61
+ "avl-tree-typed": "^1.37.3",
62
62
  "benchmark": "^2.1.4",
63
- "binary-tree-typed": "^1.37.0",
64
- "bst-typed": "^1.37.0",
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.0",
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,28 +73,30 @@ 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 {BinaryTreeNodeKey} key - The `key` parameter is the identifier of the binary tree node that we want to add.
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
- override add(key: BinaryTreeNodeKey, val?: N['val']): N | null | undefined {
84
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
81
85
  // TODO support node as a param
82
- const inserted = super.add(key, val);
86
+ const inserted = super.add(keyOrNode, val);
83
87
  if (inserted) this._balancePath(inserted);
84
88
  return inserted;
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
89
- * deletion.
90
- * @param {BinaryTreeNodeKey} key - The `key` parameter represents the identifier of the binary tree node that needs to be
91
- * removed.
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`).
92
96
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
93
97
  */
94
- override delete(key: BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
95
- const deletedResults = super.delete(key);
98
+ override delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
99
+ const deletedResults = super.delete(nodeOrKey);
96
100
  for (const {needBalanced} of deletedResults) {
97
101
  if (needBalanced) {
98
102
  this._balancePath(needBalanced);
@@ -102,10 +106,10 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
102
106
  }
103
107
 
104
108
  /**
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
+ * 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.
109
113
  */
110
114
  protected _balanceFactor(node: N): number {
111
115
  if (!node.right)
@@ -118,8 +122,9 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
118
122
  }
119
123
 
120
124
  /**
121
- * The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
122
- * @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.
123
128
  */
124
129
  protected _updateHeight(node: N): void {
125
130
  if (!node.left && !node.right) node.height = 0;
@@ -131,9 +136,10 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
131
136
  }
132
137
 
133
138
  /**
134
- * The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
135
- * each node in the path from the given node to the root.
136
- * @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.
137
143
  */
138
144
  protected _balancePath(node: N): void {
139
145
  const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
@@ -175,8 +181,8 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
175
181
  }
176
182
 
177
183
  /**
178
- * The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
179
- * @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.
180
186
  */
181
187
  protected _balanceLL(A: N): void {
182
188
  const parentOfA = A.parent;
@@ -205,8 +211,8 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
205
211
  }
206
212
 
207
213
  /**
208
- * The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
209
- * @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.
210
216
  */
211
217
  protected _balanceLR(A: N): void {
212
218
  const parentOfA = A.parent;
@@ -253,8 +259,8 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
253
259
  }
254
260
 
255
261
  /**
256
- * The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
257
- * @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.
258
264
  */
259
265
  protected _balanceRR(A: N): void {
260
266
  const parentOfA = A.parent;
@@ -288,8 +294,8 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
288
294
  }
289
295
 
290
296
  /**
291
- * The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
292
- * @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.
293
299
  */
294
300
  protected _balanceRL(A: N): void {
295
301
  const parentOfA = A.parent;