min-heap-typed 1.38.6 → 1.38.8

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 (32) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +6 -6
  2. package/dist/data-structures/binary-tree/avl-tree.js +1 -1
  3. package/dist/data-structures/binary-tree/binary-tree.d.ts +45 -33
  4. package/dist/data-structures/binary-tree/binary-tree.js +80 -30
  5. package/dist/data-structures/binary-tree/bst.d.ts +6 -6
  6. package/dist/data-structures/binary-tree/bst.js +1 -1
  7. package/dist/data-structures/binary-tree/rb-tree.d.ts +3 -3
  8. package/dist/data-structures/binary-tree/tree-multiset.d.ts +6 -6
  9. package/dist/data-structures/binary-tree/tree-multiset.js +2 -2
  10. package/dist/interfaces/binary-tree.d.ts +2 -2
  11. package/package.json +2 -2
  12. package/src/data-structures/binary-tree/avl-tree.ts +10 -11
  13. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  14. package/src/data-structures/binary-tree/binary-tree.ts +107 -52
  15. package/src/data-structures/binary-tree/bst.ts +9 -9
  16. package/src/data-structures/binary-tree/rb-tree.ts +4 -7
  17. package/src/data-structures/binary-tree/tree-multiset.ts +10 -14
  18. package/src/data-structures/graph/abstract-graph.ts +10 -11
  19. package/src/data-structures/graph/directed-graph.ts +1 -2
  20. package/src/data-structures/graph/undirected-graph.ts +4 -5
  21. package/src/data-structures/hash/hash-map.ts +1 -1
  22. package/src/data-structures/hash/tree-map.ts +2 -1
  23. package/src/data-structures/hash/tree-set.ts +2 -1
  24. package/src/data-structures/linked-list/singly-linked-list.ts +1 -1
  25. package/src/data-structures/matrix/matrix.ts +1 -1
  26. package/src/data-structures/matrix/vector2d.ts +2 -1
  27. package/src/data-structures/queue/deque.ts +5 -4
  28. package/src/data-structures/queue/queue.ts +1 -1
  29. package/src/interfaces/binary-tree.ts +2 -2
  30. package/src/types/data-structures/matrix/navigator.ts +1 -1
  31. package/src/types/utils/utils.ts +1 -1
  32. package/src/types/utils/validate-type.ts +2 -2
@@ -7,13 +7,13 @@
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
9
  import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../../types';
10
- import { IBinaryTree } from '../../interfaces';
11
10
  import { MapCallback } from '../../types';
12
- export declare class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>> extends BSTNode<V, FAMILY> {
11
+ import { IBinaryTree } from '../../interfaces';
12
+ export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
13
13
  height: number;
14
14
  constructor(key: BinaryTreeNodeKey, val?: V);
15
15
  }
16
- export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IBinaryTree<N> {
16
+ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
17
17
  /**
18
18
  * This is a constructor function for an AVL tree data structure in TypeScript.
19
19
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@@ -26,11 +26,11 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
26
26
  * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
27
27
  * the new node. It is used to determine the position of the node in the binary search tree.
28
28
  * @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
29
- * type `N['val']`, which means it can be any value that is assignable to the `val` property of the
29
+ * type `V`, which means it can be any value that is assignable to the `val` property of the
30
30
  * node type `N`.
31
31
  * @returns a new AVLTreeNode object with the specified key and value.
32
32
  */
33
- createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
33
+ createNode(key: BinaryTreeNodeKey, val?: V): N;
34
34
  /**
35
35
  * The function overrides the add method of a binary tree node and balances the tree after inserting
36
36
  * a new node.
@@ -40,7 +40,7 @@ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> e
40
40
  * are adding to the binary search tree.
41
41
  * @returns The method is returning the inserted node (`N`), `null`, or `undefined`.
42
42
  */
43
- add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
43
+ add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined;
44
44
  /**
45
45
  * The function overrides the delete method of a binary tree and balances the tree after deleting a
46
46
  * node if necessary.
@@ -31,7 +31,7 @@ class AVLTree extends bst_1.BST {
31
31
  * @param {BinaryTreeNodeKey} key - The key parameter is the key value that will be associated with
32
32
  * the new node. It is used to determine the position of the node in the binary search tree.
33
33
  * @param [val] - The parameter `val` is an optional value that can be assigned to the node. It is of
34
- * type `N['val']`, which means it can be any value that is assignable to the `val` property of the
34
+ * type `V`, which means it can be any value that is assignable to the `val` property of the
35
35
  * node type `N`.
36
36
  * @returns a new AVLTreeNode object with the specified key and value.
37
37
  */
@@ -11,9 +11,9 @@ import { IBinaryTree } from '../../interfaces';
11
11
  /**
12
12
  * Represents a node in a binary tree.
13
13
  * @template V - The type of data stored in the node.
14
- * @template FAMILY - The type of the family relationship in the binary tree.
14
+ * @template N - The type of the family relationship in the binary tree.
15
15
  */
16
- export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FAMILY> = BinaryTreeNodeNested<V>> {
16
+ export declare class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> {
17
17
  /**
18
18
  * The key associated with the node.
19
19
  */
@@ -25,7 +25,7 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
25
25
  /**
26
26
  * The parent node of the current node.
27
27
  */
28
- parent: FAMILY | null | undefined;
28
+ parent: N | null | undefined;
29
29
  /**
30
30
  * Creates a new instance of BinaryTreeNode.
31
31
  * @param {BinaryTreeNodeKey} key - The key associated with the node.
@@ -36,22 +36,22 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
36
36
  /**
37
37
  * Get the left child node.
38
38
  */
39
- get left(): FAMILY | null | undefined;
39
+ get left(): N | null | undefined;
40
40
  /**
41
41
  * Set the left child node.
42
- * @param {FAMILY | null | undefined} v - The left child node.
42
+ * @param {N | null | undefined} v - The left child node.
43
43
  */
44
- set left(v: FAMILY | null | undefined);
44
+ set left(v: N | null | undefined);
45
45
  private _right;
46
46
  /**
47
47
  * Get the right child node.
48
48
  */
49
- get right(): FAMILY | null | undefined;
49
+ get right(): N | null | undefined;
50
50
  /**
51
51
  * Set the right child node.
52
- * @param {FAMILY | null | undefined} v - The right child node.
52
+ * @param {N | null | undefined} v - The right child node.
53
53
  */
54
- set right(v: FAMILY | null | undefined);
54
+ set right(v: N | null | undefined);
55
55
  /**
56
56
  * Get the position of the node within its family.
57
57
  * @returns {FamilyPosition} - The family position of the node.
@@ -62,13 +62,22 @@ export declare class BinaryTreeNode<V = any, FAMILY extends BinaryTreeNode<V, FA
62
62
  * Represents a binary tree data structure.
63
63
  * @template N - The type of the binary tree's nodes.
64
64
  */
65
- export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> implements IBinaryTree<N> {
66
- private _loopType;
65
+ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>> implements IBinaryTree<V, N> {
67
66
  /**
68
67
  * Creates a new instance of BinaryTree.
69
68
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
70
69
  */
71
70
  constructor(options?: BinaryTreeOptions);
71
+ private _iterationType;
72
+ /**
73
+ * Get the iteration type used in the binary tree.
74
+ */
75
+ get iterationType(): IterationType;
76
+ /**
77
+ * Set the iteration type for the binary tree.
78
+ * @param {IterationType} v - The new iteration type to set.
79
+ */
80
+ set iterationType(v: IterationType);
72
81
  private _root;
73
82
  /**
74
83
  * Get the root node of the binary tree.
@@ -79,22 +88,13 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
79
88
  * Get the number of nodes in the binary tree.
80
89
  */
81
90
  get size(): number;
82
- /**
83
- * Get the iteration type used in the binary tree.
84
- */
85
- get iterationType(): IterationType;
86
- /**
87
- * Set the iteration type for the binary tree.
88
- * @param {IterationType} v - The new iteration type to set.
89
- */
90
- set iterationType(v: IterationType);
91
91
  /**
92
92
  * Creates a new instance of BinaryTreeNode with the given key and value.
93
93
  * @param {BinaryTreeNodeKey} key - The key for the new node.
94
- * @param {N['val']} val - The value for the new node.
94
+ * @param {V} val - The value for the new node.
95
95
  * @returns {N} - The newly created BinaryTreeNode.
96
96
  */
97
- createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
97
+ createNode(key: BinaryTreeNodeKey, val?: V): N;
98
98
  /**
99
99
  * Clear the binary tree, removing all nodes.
100
100
  */
@@ -107,31 +107,31 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
107
107
  /**
108
108
  * Add a node with the given key and value to the binary tree.
109
109
  * @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
110
- * @param {N['val']} val - The value for the new node (optional).
110
+ * @param {V} val - The value for the new node (optional).
111
111
  * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
112
112
  */
113
- add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
113
+ add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined;
114
114
  /**
115
115
  * The `addMany` function takes an array of binary tree node IDs or nodes, and optionally an array of corresponding data
116
116
  * values, and adds them to the binary tree.
117
117
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
118
118
  * objects, or null values.
119
- * @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to
119
+ * @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
120
120
  * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
121
121
  * the value of the nodes will be `undefined`.
122
122
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
123
123
  */
124
- addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: N['val'][]): (N | null | undefined)[];
124
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], values?: V[]): (N | null | undefined)[];
125
125
  /**
126
126
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
127
127
  * @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
128
128
  * `BinaryTreeNodeKey` or `N` values.
129
- * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
129
+ * @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
130
130
  * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
131
131
  * array. Each value in the `data` array will be assigned to the
132
132
  * @returns The method is returning a boolean value.
133
133
  */
134
- refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N[] | Array<N['val']>): boolean;
134
+ refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean;
135
135
  delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[];
136
136
  delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
137
137
  /**
@@ -285,10 +285,6 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
285
285
  * @param callback - The `callback` parameter is a function that will be called for each node in the
286
286
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
287
287
  * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
288
- * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
289
- * whether to include the level of each node in the callback function. If `withLevel` is set
290
- * to `true`, the level of each node will be passed as an argument to the callback function. If
291
- * `withLevel` is
292
288
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
293
289
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
294
290
  * will not be performed and an empty array will be returned.
@@ -296,7 +292,23 @@ export declare class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTr
296
292
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
297
293
  * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
298
294
  */
299
- bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, withLevel?: boolean, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
295
+ bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
296
+ /**
297
+ * The `listLevels` function takes a binary tree node and a callback function, and returns an array
298
+ * of arrays representing the levels of the tree.
299
+ * @param {C} callback - The `callback` parameter is a function that will be called on each node in
300
+ * the tree. It takes a node as input and returns a value. The return type of the callback function
301
+ * is determined by the generic type `C`.
302
+ * @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
303
+ * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
304
+ * from the root node of the binary tree.
305
+ * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
306
+ * recursively or iteratively. It can have two possible values:
307
+ * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
308
+ * level in a binary tree. Each inner array contains the return type of the provided callback
309
+ * function `C` applied to the nodes at that level.
310
+ */
311
+ listLevels<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[][];
300
312
  /**
301
313
  * The function returns the predecessor node of a given node in a binary tree.
302
314
  * @param {N} node - The parameter "node" represents a node in a binary tree.
@@ -14,7 +14,7 @@ const queue_1 = require("../queue");
14
14
  /**
15
15
  * Represents a node in a binary tree.
16
16
  * @template V - The type of data stored in the node.
17
- * @template FAMILY - The type of the family relationship in the binary tree.
17
+ * @template N - The type of the family relationship in the binary tree.
18
18
  */
19
19
  class BinaryTreeNode {
20
20
  /**
@@ -34,7 +34,7 @@ class BinaryTreeNode {
34
34
  }
35
35
  /**
36
36
  * Set the left child node.
37
- * @param {FAMILY | null | undefined} v - The left child node.
37
+ * @param {N | null | undefined} v - The left child node.
38
38
  */
39
39
  set left(v) {
40
40
  if (v) {
@@ -50,7 +50,7 @@ class BinaryTreeNode {
50
50
  }
51
51
  /**
52
52
  * Set the right child node.
53
- * @param {FAMILY | null | undefined} v - The right child node.
53
+ * @param {N | null | undefined} v - The right child node.
54
54
  */
55
55
  set right(v) {
56
56
  if (v) {
@@ -87,7 +87,7 @@ class BinaryTree {
87
87
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
88
88
  */
89
89
  constructor(options) {
90
- this._loopType = types_1.IterationType.ITERATIVE;
90
+ this._iterationType = types_1.IterationType.ITERATIVE;
91
91
  this._root = null;
92
92
  this._size = 0;
93
93
  /**
@@ -100,38 +100,38 @@ class BinaryTree {
100
100
  this._defaultCallbackByKey = node => node.key;
101
101
  if (options !== undefined) {
102
102
  const { iterationType = types_1.IterationType.ITERATIVE } = options;
103
- this._loopType = iterationType;
103
+ this._iterationType = iterationType;
104
104
  }
105
105
  }
106
106
  /**
107
- * Get the root node of the binary tree.
107
+ * Get the iteration type used in the binary tree.
108
108
  */
109
- get root() {
110
- return this._root;
109
+ get iterationType() {
110
+ return this._iterationType;
111
111
  }
112
112
  /**
113
- * Get the number of nodes in the binary tree.
113
+ * Set the iteration type for the binary tree.
114
+ * @param {IterationType} v - The new iteration type to set.
114
115
  */
115
- get size() {
116
- return this._size;
116
+ set iterationType(v) {
117
+ this._iterationType = v;
117
118
  }
118
119
  /**
119
- * Get the iteration type used in the binary tree.
120
+ * Get the root node of the binary tree.
120
121
  */
121
- get iterationType() {
122
- return this._loopType;
122
+ get root() {
123
+ return this._root;
123
124
  }
124
125
  /**
125
- * Set the iteration type for the binary tree.
126
- * @param {IterationType} v - The new iteration type to set.
126
+ * Get the number of nodes in the binary tree.
127
127
  */
128
- set iterationType(v) {
129
- this._loopType = v;
128
+ get size() {
129
+ return this._size;
130
130
  }
131
131
  /**
132
132
  * Creates a new instance of BinaryTreeNode with the given key and value.
133
133
  * @param {BinaryTreeNodeKey} key - The key for the new node.
134
- * @param {N['val']} val - The value for the new node.
134
+ * @param {V} val - The value for the new node.
135
135
  * @returns {N} - The newly created BinaryTreeNode.
136
136
  */
137
137
  createNode(key, val) {
@@ -154,7 +154,7 @@ class BinaryTree {
154
154
  /**
155
155
  * Add a node with the given key and value to the binary tree.
156
156
  * @param {BinaryTreeNodeKey | N | null} keyOrNode - The key or node to add to the binary tree.
157
- * @param {N['val']} val - The value for the new node (optional).
157
+ * @param {V} val - The value for the new node (optional).
158
158
  * @returns {N | null | undefined} - The inserted node, or null if nothing was inserted, or undefined if the operation failed.
159
159
  */
160
160
  add(keyOrNode, val) {
@@ -219,7 +219,7 @@ class BinaryTree {
219
219
  * values, and adds them to the binary tree.
220
220
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of BinaryTreeNodeKey or BinaryTreeNode
221
221
  * objects, or null values.
222
- * @param {N['val'][]} [values] - The `values` parameter is an optional array of values (`N['val'][]`) that corresponds to
222
+ * @param {V[]} [values] - The `values` parameter is an optional array of values (`V[]`) that corresponds to
223
223
  * the nodes or node IDs being added. It is used to set the value of each node being added. If `values` is not provided,
224
224
  * the value of the nodes will be `undefined`.
225
225
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
@@ -241,7 +241,7 @@ class BinaryTree {
241
241
  * The `refill` function clears the binary tree and adds multiple nodes with the given IDs or nodes and optional data.
242
242
  * @param {(BinaryTreeNodeKey | N)[]} keysOrNodes - The `keysOrNodes` parameter is an array that can contain either
243
243
  * `BinaryTreeNodeKey` or `N` values.
244
- * @param {N[] | Array<N['val']>} [data] - The `data` parameter is an optional array of values that will be assigned to
244
+ * @param {N[] | Array<V>} [data] - The `data` parameter is an optional array of values that will be assigned to
245
245
  * the nodes being added. If provided, the length of the `data` array should be equal to the length of the `keysOrNodes`
246
246
  * array. Each value in the `data` array will be assigned to the
247
247
  * @returns The method is returning a boolean value.
@@ -821,10 +821,6 @@ class BinaryTree {
821
821
  * @param callback - The `callback` parameter is a function that will be called for each node in the
822
822
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
823
823
  * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
824
- * @param {boolean} [withLevel=false] - The `withLevel` parameter is a boolean flag that determines
825
- * whether to include the level of each node in the callback function. If `withLevel` is set
826
- * to `true`, the level of each node will be passed as an argument to the callback function. If
827
- * `withLevel` is
828
824
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
829
825
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
830
826
  * will not be performed and an empty array will be returned.
@@ -832,13 +828,65 @@ class BinaryTree {
832
828
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
833
829
  * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
834
830
  */
835
- bfs(callback = this._defaultCallbackByKey, withLevel = false, beginRoot = this.root, iterationType = this.iterationType) {
831
+ bfs(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
836
832
  if (!beginRoot)
837
833
  return [];
838
834
  const ans = [];
835
+ if (iterationType === types_1.IterationType.RECURSIVE) {
836
+ const queue = new queue_1.Queue([beginRoot]);
837
+ function traverse(level) {
838
+ if (queue.size === 0)
839
+ return;
840
+ const current = queue.shift();
841
+ ans.push(callback(current));
842
+ if (current.left)
843
+ queue.push(current.left);
844
+ if (current.right)
845
+ queue.push(current.right);
846
+ traverse(level + 1);
847
+ }
848
+ traverse(0);
849
+ }
850
+ else {
851
+ const queue = new queue_1.Queue([beginRoot]);
852
+ while (queue.size > 0) {
853
+ const levelSize = queue.size;
854
+ for (let i = 0; i < levelSize; i++) {
855
+ const current = queue.shift();
856
+ ans.push(callback(current));
857
+ if (current.left)
858
+ queue.push(current.left);
859
+ if (current.right)
860
+ queue.push(current.right);
861
+ }
862
+ }
863
+ }
864
+ return ans;
865
+ }
866
+ /**
867
+ * The `listLevels` function takes a binary tree node and a callback function, and returns an array
868
+ * of arrays representing the levels of the tree.
869
+ * @param {C} callback - The `callback` parameter is a function that will be called on each node in
870
+ * the tree. It takes a node as input and returns a value. The return type of the callback function
871
+ * is determined by the generic type `C`.
872
+ * @param {N | null} beginRoot - The `beginRoot` parameter represents the starting node of the binary tree
873
+ * traversal. It can be any node in the binary tree. If no node is provided, the traversal will start
874
+ * from the root node of the binary tree.
875
+ * @param iterationType - The `iterationType` parameter determines whether the tree traversal is done
876
+ * recursively or iteratively. It can have two possible values:
877
+ * @returns The function `listLevels` returns an array of arrays, where each inner array represents a
878
+ * level in a binary tree. Each inner array contains the return type of the provided callback
879
+ * function `C` applied to the nodes at that level.
880
+ */
881
+ listLevels(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
882
+ if (!beginRoot)
883
+ return [];
884
+ const levelsNodes = [];
839
885
  if (iterationType === types_1.IterationType.RECURSIVE) {
840
886
  const _recursive = (node, level) => {
841
- callback && ans.push(callback(node, withLevel ? level : undefined));
887
+ if (!levelsNodes[level])
888
+ levelsNodes[level] = [];
889
+ levelsNodes[level].push(callback(node));
842
890
  if (node.left)
843
891
  _recursive(node.left, level + 1);
844
892
  if (node.right)
@@ -851,14 +899,16 @@ class BinaryTree {
851
899
  while (stack.length > 0) {
852
900
  const head = stack.pop();
853
901
  const [node, level] = head;
854
- callback && ans.push(callback(node, withLevel ? level : undefined));
902
+ if (!levelsNodes[level])
903
+ levelsNodes[level] = [];
904
+ levelsNodes[level].push(callback(node));
855
905
  if (node.right)
856
906
  stack.push([node.right, level + 1]);
857
907
  if (node.left)
858
908
  stack.push([node.left, level + 1]);
859
909
  }
860
910
  }
861
- return ans;
911
+ return levelsNodes;
862
912
  }
863
913
  /**
864
914
  * The function returns the predecessor node of a given node in a binary tree.
@@ -9,10 +9,10 @@ import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCa
9
9
  import { CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
12
- export declare class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> {
12
+ export declare class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
13
13
  constructor(key: BinaryTreeNodeKey, val?: V);
14
14
  }
15
- export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> {
15
+ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
16
16
  /**
17
17
  * The constructor function initializes a binary search tree object with an optional comparator
18
18
  * function.
@@ -28,7 +28,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
28
28
  * represents the value associated with the node in a binary search tree.
29
29
  * @returns a new instance of the BSTNode class with the specified key and value.
30
30
  */
31
- createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
31
+ createNode(key: BinaryTreeNodeKey, val?: V): N;
32
32
  /**
33
33
  * The `add` function in a binary search tree class inserts a new node with a given key and value
34
34
  * into the tree.
@@ -39,7 +39,7 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
39
39
  * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
40
40
  * was not added or if the parameters were invalid, it returns null or undefined.
41
41
  */
42
- add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
42
+ add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined;
43
43
  /**
44
44
  * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
45
45
  * maintaining balance.
@@ -47,13 +47,13 @@ export declare class BST<N extends BSTNode<N['val'], N> = BSTNode> extends Binar
47
47
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
48
48
  * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
49
49
  * `null
50
- * @param {N['val'][]} data - The values of tree nodes
50
+ * @param {V[]} data - The values of tree nodes
51
51
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
52
52
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
53
53
  * It can have two possible values:
54
54
  * @returns The `addMany` function returns an array of `N`, `null`, or `undefined` values.
55
55
  */
56
- addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
56
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[], isBalanceAdd?: boolean, iterationType?: IterationType): (N | null | undefined)[];
57
57
  /**
58
58
  * The function returns the first node in the binary tree that matches the given node property and
59
59
  * callback.
@@ -130,7 +130,7 @@ class BST extends binary_tree_1.BinaryTree {
130
130
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
131
131
  * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
132
132
  * `null
133
- * @param {N['val'][]} data - The values of tree nodes
133
+ * @param {V[]} data - The values of tree nodes
134
134
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
135
135
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
136
136
  * It can have two possible values:
@@ -1,13 +1,13 @@
1
1
  import { BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions } from '../../types';
2
2
  import { IBinaryTree } from '../../interfaces';
3
3
  import { BST, BSTNode } from './bst';
4
- export declare class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<V, FAMILY> {
4
+ export declare class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
5
5
  constructor(key: BinaryTreeNodeKey, val?: V);
6
6
  private _color;
7
7
  get color(): RBColor;
8
8
  set color(value: RBColor);
9
9
  }
10
- export declare class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IBinaryTree<N> {
10
+ export declare class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>> extends BST<V, N> implements IBinaryTree<V, N> {
11
11
  constructor(options?: RBTreeOptions);
12
- createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
12
+ createNode(key: BinaryTreeNodeKey, val?: V): N;
13
13
  }
@@ -9,7 +9,7 @@ import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } f
9
9
  import { BinaryTreeDeletedResult, IterationType, MapCallback } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
- export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
12
+ export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
13
13
  count: number;
14
14
  /**
15
15
  * The constructor function initializes a BinaryTreeNode object with a key, value, and count.
@@ -26,7 +26,7 @@ export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V
26
26
  /**
27
27
  * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
28
28
  */
29
- export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode> extends AVLTree<N> implements IBinaryTree<N> {
29
+ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>> extends AVLTree<V, N> implements IBinaryTree<V, N> {
30
30
  /**
31
31
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
32
32
  * merge duplicated values.
@@ -45,7 +45,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
45
45
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
46
46
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
47
47
  */
48
- createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N;
48
+ createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N;
49
49
  /**
50
50
  * The `add` function adds a new node to a binary search tree, updating the count if the key already
51
51
  * exists, and balancing the tree if necessary.
@@ -59,7 +59,7 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
59
59
  * count is specified, the default count will be 1.
60
60
  * @returns The function `add` returns a value of type `N | null | undefined`.
61
61
  */
62
- add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count?: number): N | null | undefined;
62
+ add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count?: number): N | null | undefined;
63
63
  /**
64
64
  * The function adds a new node to a binary tree if there is an available slot in the parent node.
65
65
  * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to
@@ -74,12 +74,12 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
74
74
  * inserted nodes.
75
75
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
76
76
  * added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
77
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
77
+ * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
78
78
  * to the keys or nodes being added to the multiset. It is used to associate additional data with
79
79
  * each key or node.
80
80
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
81
81
  */
82
- addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: N['val'][]): (N | null | undefined)[];
82
+ addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[];
83
83
  /**
84
84
  * The `perfectlyBalance` function in TypeScript takes a sorted array of nodes and builds a balanced
85
85
  * binary search tree using either a recursive or iterative approach.
@@ -177,7 +177,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
177
177
  * inserted nodes.
178
178
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
179
179
  * added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
180
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
180
+ * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
181
181
  * to the keys or nodes being added to the multiset. It is used to associate additional data with
182
182
  * each key or node.
183
183
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
@@ -191,7 +191,7 @@ class TreeMultiset extends avl_tree_1.AVLTree {
191
191
  continue;
192
192
  }
193
193
  if (keyOrNode === null) {
194
- inserted.push(this.add(NaN, null, 0));
194
+ inserted.push(this.add(NaN, undefined, 0));
195
195
  continue;
196
196
  }
197
197
  inserted.push(this.add(keyOrNode, data === null || data === void 0 ? void 0 : data[i], 1));
@@ -1,6 +1,6 @@
1
1
  import { BinaryTreeNode } from '../data-structures';
2
- import { BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback } from '../types';
3
- export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
2
+ import { BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback } from '../types';
3
+ export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
4
4
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
5
5
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
6
6
  delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "min-heap-typed",
3
- "version": "1.38.6",
3
+ "version": "1.38.8",
4
4
  "description": "Min Heap. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -131,6 +131,6 @@
131
131
  "typescript": "^4.9.5"
132
132
  },
133
133
  "dependencies": {
134
- "data-structure-typed": "^1.38.6"
134
+ "data-structure-typed": "^1.38.8"
135
135
  }
136
136
  }