data-structure-typed 1.38.5 → 1.38.7

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 (46) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +14 -8
  3. package/dist/cjs/data-structures/binary-tree/avl-tree.js +10 -5
  4. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +59 -107
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js +72 -81
  7. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/bst.d.ts +13 -13
  9. package/dist/cjs/data-structures/binary-tree/bst.js +14 -14
  10. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  11. package/dist/cjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
  12. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  13. package/dist/cjs/data-structures/binary-tree/tree-multiset.d.ts +15 -11
  14. package/dist/cjs/data-structures/binary-tree/tree-multiset.js +11 -7
  15. package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
  16. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  17. package/dist/cjs/interfaces/binary-tree.d.ts +3 -3
  18. package/dist/cjs/types/helpers.d.ts +2 -0
  19. package/dist/cjs/types/helpers.js.map +1 -1
  20. package/dist/mjs/data-structures/binary-tree/avl-tree.d.ts +14 -8
  21. package/dist/mjs/data-structures/binary-tree/avl-tree.js +10 -5
  22. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +59 -107
  23. package/dist/mjs/data-structures/binary-tree/binary-tree.js +72 -81
  24. package/dist/mjs/data-structures/binary-tree/bst.d.ts +13 -13
  25. package/dist/mjs/data-structures/binary-tree/bst.js +14 -14
  26. package/dist/mjs/data-structures/binary-tree/rb-tree.d.ts +3 -3
  27. package/dist/mjs/data-structures/binary-tree/tree-multiset.d.ts +15 -11
  28. package/dist/mjs/data-structures/binary-tree/tree-multiset.js +11 -7
  29. package/dist/mjs/interfaces/binary-tree.d.ts +3 -3
  30. package/dist/mjs/types/helpers.d.ts +2 -0
  31. package/dist/umd/index.global.js +1 -1
  32. package/dist/umd/index.global.js.map +1 -1
  33. package/package.json +5 -5
  34. package/src/data-structures/binary-tree/avl-tree.ts +22 -13
  35. package/src/data-structures/binary-tree/binary-tree.ts +155 -111
  36. package/src/data-structures/binary-tree/bst.ts +26 -26
  37. package/src/data-structures/binary-tree/rb-tree.ts +6 -9
  38. package/src/data-structures/binary-tree/tree-multiset.ts +24 -19
  39. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -1
  40. package/src/interfaces/binary-tree.ts +3 -3
  41. package/src/types/helpers.ts +4 -0
  42. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +2 -2
  43. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +1 -1
  44. package/test/unit/data-structures/binary-tree/bst.test.ts +1 -1
  45. package/test/unit/data-structures/binary-tree/overall.test.ts +20 -20
  46. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +15 -15
@@ -18,13 +18,13 @@ import {BinaryTree, BinaryTreeNode} from './binary-tree';
18
18
  import {IBinaryTree} from '../../interfaces';
19
19
  import {Queue} from '../queue';
20
20
 
21
- export class BSTNode<V = any, FAMILY extends BSTNode<V, FAMILY> = BSTNodeNested<V>> extends BinaryTreeNode<V, FAMILY> {
21
+ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extends BinaryTreeNode<V, N> {
22
22
  constructor(key: BinaryTreeNodeKey, val?: V) {
23
23
  super(key, val);
24
24
  }
25
25
  }
26
26
 
27
- export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N> implements IBinaryTree<N> {
27
+ export class BST<V = any, N extends BSTNode<V, N> = BSTNode> extends BinaryTree<V, N> implements IBinaryTree<V, N> {
28
28
  /**
29
29
  * The constructor function initializes a binary search tree object with an optional comparator
30
30
  * function.
@@ -49,8 +49,8 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
49
49
  * represents the value associated with the node in a binary search tree.
50
50
  * @returns a new instance of the BSTNode class with the specified key and value.
51
51
  */
52
- override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
53
- return new BSTNode<N['val'], N>(key, val) as N;
52
+ override createNode(key: BinaryTreeNodeKey, val?: V): N {
53
+ return new BSTNode<V, N>(key, val) as N;
54
54
  }
55
55
 
56
56
  /**
@@ -63,7 +63,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
63
63
  * @returns the inserted node (N) if it was successfully added to the binary search tree. If the node
64
64
  * was not added or if the parameters were invalid, it returns null or undefined.
65
65
  */
66
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
66
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
67
67
  // TODO support node as a parameter
68
68
  let inserted: N | null = null;
69
69
  let newNode: N | null = null;
@@ -132,11 +132,11 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
132
132
  /**
133
133
  * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
134
134
  * maintaining balance.
135
- * @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function
135
+ * @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
136
136
  * represents an array of keys or nodes that need to be added to the binary search tree. It can be an
137
137
  * array of `BinaryTreeNodeKey` or `N` (which represents the node type in the binary search tree) or
138
138
  * `null
139
- * @param {N['val'][]} data - The values of tree nodes
139
+ * @param {V[]} data - The values of tree nodes
140
140
  * @param {boolean} isBalanceAdd - If true the nodes will be balance inserted in binary search method.
141
141
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used.
142
142
  * It can have two possible values:
@@ -145,7 +145,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
145
145
 
146
146
  override addMany(
147
147
  keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
148
- data?: N['val'][],
148
+ data?: V[],
149
149
  isBalanceAdd = true,
150
150
  iterationType = this.iterationType
151
151
  ): (N | null | undefined)[] {
@@ -174,7 +174,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
174
174
  }
175
175
 
176
176
  let sortedKeysOrNodes: (number | N | null)[] = [],
177
- sortedData: (N['val'] | undefined)[] | undefined = [];
177
+ sortedData: (V | undefined)[] | undefined = [];
178
178
 
179
179
  if (isNodeOrNullTuple(combinedArr)) {
180
180
  sorted = combinedArr.sort((a, b) => a[0].key - b[0].key);
@@ -185,7 +185,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
185
185
  }
186
186
  sortedKeysOrNodes = sorted.map(([keyOrNode]) => keyOrNode);
187
187
  sortedData = sorted.map(([, val]) => val);
188
- const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: N['val'][]) => {
188
+ const recursive = (arr: (BinaryTreeNodeKey | null | N)[], data?: (V | undefined)[]) => {
189
189
  if (arr.length === 0) return;
190
190
 
191
191
  const mid = Math.floor((arr.length - 1) / 2);
@@ -223,7 +223,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
223
223
  /**
224
224
  * The function returns the first node in the binary tree that matches the given node property and
225
225
  * callback.
226
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the
226
+ * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
227
227
  * property of the binary tree node that you want to search for. It can be either a specific key
228
228
  * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
229
229
  * whether a node matches the desired property.
@@ -238,13 +238,13 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
238
238
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
239
239
  * matching node is found.
240
240
  */
241
- override get<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
242
- nodeProperty: BinaryTreeNodeKey | N,
241
+ override get<C extends MapCallback<N>>(
242
+ identifier: ReturnType<C> | N,
243
243
  callback: C = this._defaultCallbackByKey as C,
244
244
  beginRoot = this.root,
245
245
  iterationType = this.iterationType
246
246
  ): N | null {
247
- return this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0] ?? null;
247
+ return this.getNodes(identifier, callback, true, beginRoot, iterationType)[0] ?? null;
248
248
  }
249
249
 
250
250
  /**
@@ -271,7 +271,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
271
271
  /**
272
272
  * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
273
273
  * using either recursive or iterative traversal.
274
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property
274
+ * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
275
275
  * of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
276
276
  * generic type `N`.
277
277
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
@@ -289,8 +289,8 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
289
289
  * traverse the binary tree. It can have one of the following values:
290
290
  * @returns an array of nodes (N[]).
291
291
  */
292
- override getNodes<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
293
- nodeProperty: BinaryTreeNodeKey | N,
292
+ override getNodes<C extends MapCallback<N>>(
293
+ identifier: ReturnType<C> | N,
294
294
  callback: C = this._defaultCallbackByKey as C,
295
295
  onlyOne = false,
296
296
  beginRoot: N | null = this.root,
@@ -302,7 +302,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
302
302
  if (iterationType === IterationType.RECURSIVE) {
303
303
  const _traverse = (cur: N) => {
304
304
  const callbackResult = callback(cur);
305
- if (callbackResult === nodeProperty) {
305
+ if (callbackResult === identifier) {
306
306
  ans.push(cur);
307
307
  if (onlyOne) return;
308
308
  }
@@ -310,8 +310,8 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
310
310
  if (!cur.left && !cur.right) return;
311
311
  // TODO potential bug
312
312
  if (callback === this._defaultCallbackByKey) {
313
- if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && _traverse(cur.left);
314
- if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && _traverse(cur.right);
313
+ if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && _traverse(cur.left);
314
+ if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && _traverse(cur.right);
315
315
  } else {
316
316
  cur.left && _traverse(cur.left);
317
317
  cur.right && _traverse(cur.right);
@@ -325,14 +325,14 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
325
325
  const cur = queue.shift();
326
326
  if (cur) {
327
327
  const callbackResult = callback(cur);
328
- if (callbackResult === nodeProperty) {
328
+ if (callbackResult === identifier) {
329
329
  ans.push(cur);
330
330
  if (onlyOne) return ans;
331
331
  }
332
332
  // TODO potential bug
333
333
  if (callback === this._defaultCallbackByKey) {
334
- if (this._compare(cur.key, nodeProperty as number) === CP.gt) cur.left && queue.push(cur.left);
335
- if (this._compare(cur.key, nodeProperty as number) === CP.lt) cur.right && queue.push(cur.right);
334
+ if (this._compare(cur.key, identifier as number) === CP.gt) cur.left && queue.push(cur.left);
335
+ if (this._compare(cur.key, identifier as number) === CP.lt) cur.right && queue.push(cur.right);
336
336
  } else {
337
337
  cur.left && queue.push(cur.left);
338
338
  cur.right && queue.push(cur.right);
@@ -355,7 +355,7 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
355
355
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
356
356
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
357
357
  * of the following values:
358
- * @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the
358
+ * @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
359
359
  * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
360
360
  * start. It can be either a reference to a specific node (`N`), the key of a node
361
361
  * (`BinaryTreeNodeKey`), or `null` to
@@ -363,10 +363,10 @@ export class BST<N extends BSTNode<N['val'], N> = BSTNode> extends BinaryTree<N>
363
363
  * done recursively or iteratively. It can have two possible values:
364
364
  * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
365
365
  */
366
- lesserOrGreaterTraverse<C extends MapCallback<N> = MapCallback<N, BinaryTreeNodeKey>>(
366
+ lesserOrGreaterTraverse<C extends MapCallback<N>>(
367
367
  callback: C = this._defaultCallbackByKey as C,
368
368
  lesserOrGreater: CP = CP.lt,
369
- targetNode: N | BinaryTreeNodeKey | null = this.root,
369
+ targetNode: BinaryTreeNodeKey | N | null = this.root,
370
370
  iterationType = this.iterationType
371
371
  ): ReturnType<C>[] {
372
372
  if (typeof targetNode === 'number') targetNode = this.get(targetNode);
@@ -2,10 +2,7 @@ import {BinaryTreeNodeKey, RBColor, RBTreeNodeNested, RBTreeOptions} from '../..
2
2
  import {IBinaryTree} from '../../interfaces';
3
3
  import {BST, BSTNode} from './bst';
4
4
 
5
- export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNodeNested<V>> extends BSTNode<
6
- V,
7
- FAMILY
8
- > {
5
+ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V>> extends BSTNode<V, N> {
9
6
  constructor(key: BinaryTreeNodeKey, val?: V) {
10
7
  super(key, val);
11
8
  this._color = RBColor.RED;
@@ -22,16 +19,16 @@ export class RBTreeNode<V = any, FAMILY extends RBTreeNode<V, FAMILY> = RBTreeNo
22
19
  }
23
20
  }
24
21
 
25
- export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<N> implements IBinaryTree<N> {
22
+ export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode> extends BST<V, N> implements IBinaryTree<V, N> {
26
23
  constructor(options?: RBTreeOptions) {
27
24
  super(options);
28
25
  }
29
26
 
30
- override createNode(key: BinaryTreeNodeKey, val?: N['val']): N {
27
+ override createNode(key: BinaryTreeNodeKey, val?: V): N {
31
28
  return new RBTreeNode(key, val) as N;
32
29
  }
33
30
 
34
- // override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined {
31
+ // override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V): N | null | undefined {
35
32
  // const inserted = super.add(keyOrNode, val);
36
33
  // if (inserted) this._fixInsertViolation(inserted);
37
34
  // return inserted;
@@ -205,8 +202,8 @@ export class RBTree<N extends RBTreeNode<N['val'], N> = RBTreeNode> extends BST<
205
202
  // node.right = null;
206
203
  // }
207
204
  //
208
- // override delete(nodeOrKey: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] {
209
- // const node = this.get(nodeOrKey);
205
+ // override delete(keyOrNode: BinaryTreeNodeKey | N): BinaryTreeDeletedResult<N>[] {
206
+ // const node = this.get(keyOrNode);
210
207
  // const result: BinaryTreeDeletedResult<N>[] = [{deleted: undefined, needBalanced: null}];
211
208
  // if (!node) return result; // Node does not exist
212
209
  //
@@ -6,14 +6,14 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type {BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
9
- import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType} from '../../types';
9
+ import {BinaryTreeDeletedResult, CP, FamilyPosition, IterationType, MapCallback} from '../../types';
10
10
  import {IBinaryTree} from '../../interfaces';
11
11
  import {AVLTree, AVLTreeNode} from './avl-tree';
12
12
 
13
13
  export class TreeMultisetNode<
14
14
  V = any,
15
- FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>
16
- > extends AVLTreeNode<V, FAMILY> {
15
+ N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>
16
+ > extends AVLTreeNode<V, N> {
17
17
  count: number;
18
18
 
19
19
  /**
@@ -35,9 +35,9 @@ export class TreeMultisetNode<
35
35
  /**
36
36
  * 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.
37
37
  */
38
- export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
39
- extends AVLTree<N>
40
- implements IBinaryTree<N>
38
+ export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode>
39
+ extends AVLTree<V, N>
40
+ implements IBinaryTree<V, N>
41
41
  {
42
42
  /**
43
43
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
@@ -64,7 +64,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
64
64
  * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
65
65
  * @returns A new instance of the BSTNode class with the specified key, value, and count (if provided).
66
66
  */
67
- override createNode(key: BinaryTreeNodeKey, val?: N['val'], count?: number): N {
67
+ override createNode(key: BinaryTreeNodeKey, val?: V, count?: number): N {
68
68
  return new TreeMultisetNode(key, val, count) as N;
69
69
  }
70
70
 
@@ -81,7 +81,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
81
81
  * count is specified, the default count will be 1.
82
82
  * @returns The function `add` returns a value of type `N | null | undefined`.
83
83
  */
84
- override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val'], count = 1): N | null | undefined {
84
+ override add(keyOrNode: BinaryTreeNodeKey | N | null, val?: V, count = 1): N | null | undefined {
85
85
  let inserted: N | null | undefined = undefined,
86
86
  newNode: N | null;
87
87
  if (keyOrNode instanceof TreeMultisetNode) {
@@ -187,15 +187,12 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
187
187
  * inserted nodes.
188
188
  * @param {(BinaryTreeNodeKey | null)[] | (N | null)[]} keysOrNodes - An array of keys or nodes to be
189
189
  * added to the multiset. Each element can be either a BinaryTreeNodeKey or a TreeMultisetNode.
190
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values that correspond
190
+ * @param {V[]} [data] - The `data` parameter is an optional array of values that correspond
191
191
  * to the keys or nodes being added to the multiset. It is used to associate additional data with
192
192
  * each key or node.
193
193
  * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
194
194
  */
195
- override addMany(
196
- keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[],
197
- data?: N['val'][]
198
- ): (N | null | undefined)[] {
195
+ override addMany(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: V[]): (N | null | undefined)[] {
199
196
  const inserted: (N | null | undefined)[] = [];
200
197
 
201
198
  for (let i = 0; i < keysOrNodes.length; i++) {
@@ -207,7 +204,7 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
207
204
  }
208
205
 
209
206
  if (keyOrNode === null) {
210
- inserted.push(this.add(NaN, null, 0));
207
+ inserted.push(this.add(NaN, undefined, 0));
211
208
  continue;
212
209
  }
213
210
 
@@ -265,20 +262,28 @@ export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultiset
265
262
  /**
266
263
  * The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
267
264
  * node along with the parent node that needs to be balanced.
268
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
269
- * (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
270
- * from the binary tree.
265
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
266
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
267
+ * searching for. It can be a specific key value or any other property of the node.
268
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
269
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
270
+ * included in the result. The `callback` parameter has a default value of
271
+ * `this._defaultCallbackByKey`
271
272
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
272
273
  * being deleted. If set to true, the count of the node will not be considered and the node will be
273
274
  * deleted regardless of its count. If set to false (default), the count of the node will be
274
275
  * decremented by 1 and
275
276
  * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
276
277
  */
277
- override delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount = false): BinaryTreeDeletedResult<N>[] {
278
+ override delete<C extends MapCallback<N>>(
279
+ identifier: ReturnType<C>,
280
+ callback: C = this._defaultCallbackByKey as C,
281
+ ignoreCount = false
282
+ ): BinaryTreeDeletedResult<N>[] {
278
283
  const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
279
284
  if (!this.root) return bstDeletedResult;
280
285
 
281
- const curr: N | null = this.get(nodeOrKey);
286
+ const curr: N | null = this.get(identifier, callback);
282
287
  if (!curr) return bstDeletedResult;
283
288
 
284
289
  const parent: N | null = curr?.parent ? curr.parent : null;
@@ -594,7 +594,6 @@ export class DoublyLinkedList<E = any> {
594
594
  return false;
595
595
  }
596
596
 
597
-
598
597
  /**
599
598
  * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
600
599
  * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
@@ -1,10 +1,10 @@
1
1
  import {BinaryTreeNode} from '../data-structures';
2
- import {BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../types';
2
+ import {BinaryTreeDeletedResult, BinaryTreeNodeKey, BinaryTreeNodeNested, MapCallback} from '../types';
3
3
 
4
- export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
4
+ export interface IBinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNodeNested<V>> {
5
5
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
6
6
 
7
7
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
8
8
 
9
- delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
9
+ delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
10
10
  }
@@ -1,9 +1,13 @@
1
+ import {BinaryTreeNodeKey} from './data-structures';
2
+
1
3
  export type Comparator<T> = (a: T, b: T) => number;
2
4
 
3
5
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
4
6
 
5
7
  export type MapCallback<N, D = any> = (node: N) => D;
6
8
 
9
+ export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
10
+
7
11
  export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
8
12
 
9
13
  export enum CP {
@@ -3,7 +3,7 @@ import {AVLTree, AVLTreeNode, CP} from '../../../../src';
3
3
  describe('AVL Tree Test', () => {
4
4
  it('should perform various operations on a AVL Tree', () => {
5
5
  const arr = [11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5];
6
- const tree = new AVLTree<AVLTreeNode<number>>();
6
+ const tree = new AVLTree<number>();
7
7
 
8
8
  for (const i of arr) tree.add(i, i);
9
9
 
@@ -110,7 +110,7 @@ describe('AVL Tree Test', () => {
110
110
  });
111
111
 
112
112
  describe('AVLTree APIs test', () => {
113
- const avl = new AVLTree<AVLTreeNode<{id: number; text: string}>>();
113
+ const avl = new AVLTree<{id: number; text: string}>();
114
114
  beforeEach(() => {
115
115
  avl.clear();
116
116
  });
@@ -196,7 +196,7 @@ describe('BinaryTree Morris Traversal', () => {
196
196
  });
197
197
 
198
198
  describe('BinaryTree APIs test', () => {
199
- const avl = new AVLTree<AVLTreeNode<{id: number; text: string}>>();
199
+ const avl = new AVLTree<{id: number; text: string}>();
200
200
  beforeEach(() => {
201
201
  avl.clear();
202
202
  });
@@ -189,7 +189,7 @@ describe('BST operations test', () => {
189
189
  });
190
190
 
191
191
  it('should perform various operations on a Binary Search Tree with object values', () => {
192
- const objBST = new BST<BSTNode<{key: number; keyA: number}>>();
192
+ const objBST = new BST<{key: number; keyA: number}>();
193
193
  expect(objBST).toBeInstanceOf(BST);
194
194
  objBST.add(11, {key: 11, keyA: 11});
195
195
  objBST.add(3, {key: 3, keyA: 3});
@@ -1,4 +1,4 @@
1
- import {AVLTree, BST, BSTNode} from '../../../../src';
1
+ import {AVLTree, BST} from '../../../../src';
2
2
 
3
3
  describe('Overall BinaryTree Test', () => {
4
4
  it('should perform various operations on BinaryTree', () => {
@@ -6,30 +6,30 @@ describe('Overall BinaryTree Test', () => {
6
6
  bst.add(11);
7
7
  bst.add(3);
8
8
  bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], undefined, false);
9
- bst.size === 16; // true
10
- expect(bst.size).toBe(16); // true
11
- bst.has(6); // true
12
- expect(bst.has(6)).toBe(true); // true
13
- bst.getHeight(6) === 2; // true
14
- bst.getHeight() === 5; // true
15
- bst.getDepth(6) === 3; // true
16
- expect(bst.getHeight(6)).toBe(2); // true
17
- expect(bst.getHeight()).toBe(5); // true
18
- expect(bst.getDepth(6)).toBe(3); // true
9
+ bst.size === 16; // true
10
+ expect(bst.size).toBe(16); // true
11
+ bst.has(6); // true
12
+ expect(bst.has(6)).toBe(true); // true
13
+ bst.getHeight(6) === 2; // true
14
+ bst.getHeight() === 5; // true
15
+ bst.getDepth(6) === 3; // true
16
+ expect(bst.getHeight(6)).toBe(2); // true
17
+ expect(bst.getHeight()).toBe(5); // true
18
+ expect(bst.getDepth(6)).toBe(3); // true
19
19
  const leftMost = bst.getLeftMost();
20
- leftMost?.key === 1; // true
20
+ leftMost?.key === 1; // true
21
21
  expect(leftMost?.key).toBe(1);
22
22
  bst.delete(6);
23
- bst.get(6); // null
23
+ bst.get(6); // null
24
24
  expect(bst.get(6)).toBeNull();
25
- bst.isAVLBalanced(); // true or false
25
+ bst.isAVLBalanced(); // true or false
26
26
  expect(bst.isAVLBalanced()).toBe(true);
27
27
  const bfsIDs: number[] = [];
28
28
  bst.bfs(node => bfsIDs.push(node.key));
29
- bfsIDs[0] === 11; // true
29
+ bfsIDs[0] === 11; // true
30
30
  expect(bfsIDs[0]).toBe(11);
31
31
 
32
- const objBST = new BST<BSTNode<{key: number; keyA: number}>>();
32
+ const objBST = new BST<{key: number; keyA: number}>();
33
33
  objBST.add(11, {key: 11, keyA: 11});
34
34
  objBST.add(3, {key: 3, keyA: 3});
35
35
 
@@ -57,10 +57,10 @@ describe('Overall BinaryTree Test', () => {
57
57
 
58
58
  const avlTree = new AVLTree();
59
59
  avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]);
60
- avlTree.isAVLBalanced(); // true
61
- expect(avlTree.isAVLBalanced()).toBe(true); // true
60
+ avlTree.isAVLBalanced(); // true
61
+ expect(avlTree.isAVLBalanced()).toBe(true); // true
62
62
  avlTree.delete(10);
63
- avlTree.isAVLBalanced(); // true
64
- expect(avlTree.isAVLBalanced()).toBe(true); // true
63
+ avlTree.isAVLBalanced(); // true
64
+ expect(avlTree.isAVLBalanced()).toBe(true); // true
65
65
  });
66
66
  });
@@ -73,7 +73,7 @@ describe('TreeMultiset operations test', () => {
73
73
  expect(bfsNodesAfterBalanced[0].key).toBe(8);
74
74
  expect(bfsNodesAfterBalanced[bfsNodesAfterBalanced.length - 1].key).toBe(16);
75
75
 
76
- const removed11 = treeMultiset.delete(11, true);
76
+ const removed11 = treeMultiset.delete(11, undefined, true);
77
77
  expect(removed11 instanceof Array);
78
78
  expect(removed11[0]);
79
79
  expect(removed11[0].deleted);
@@ -84,7 +84,7 @@ describe('TreeMultiset operations test', () => {
84
84
 
85
85
  expect(treeMultiset.getHeight(15)).toBe(1);
86
86
 
87
- const removed1 = treeMultiset.delete(1, true);
87
+ const removed1 = treeMultiset.delete(1, undefined, true);
88
88
  expect(removed1 instanceof Array);
89
89
  expect(removed1[0]);
90
90
  expect(removed1[0].deleted);
@@ -94,7 +94,7 @@ describe('TreeMultiset operations test', () => {
94
94
 
95
95
  expect(treeMultiset.getHeight()).toBe(4);
96
96
 
97
- const removed4 = treeMultiset.delete(4, true);
97
+ const removed4 = treeMultiset.delete(4, undefined, true);
98
98
  expect(removed4 instanceof Array);
99
99
  expect(removed4[0]);
100
100
  expect(removed4[0].deleted);
@@ -103,7 +103,7 @@ describe('TreeMultiset operations test', () => {
103
103
  expect(treeMultiset.isAVLBalanced()).toBe(true);
104
104
  expect(treeMultiset.getHeight()).toBe(4);
105
105
 
106
- const removed10 = treeMultiset.delete(10, true);
106
+ const removed10 = treeMultiset.delete(10, undefined, true);
107
107
  expect(removed10 instanceof Array);
108
108
  expect(removed10[0]);
109
109
  expect(removed10[0].deleted);
@@ -112,7 +112,7 @@ describe('TreeMultiset operations test', () => {
112
112
 
113
113
  expect(treeMultiset.getHeight()).toBe(3);
114
114
 
115
- const removed15 = treeMultiset.delete(15, true);
115
+ const removed15 = treeMultiset.delete(15, undefined, true);
116
116
  expect(removed15 instanceof Array);
117
117
  expect(removed15[0]);
118
118
  expect(removed15[0].deleted);
@@ -121,7 +121,7 @@ describe('TreeMultiset operations test', () => {
121
121
  expect(treeMultiset.isAVLBalanced()).toBe(true);
122
122
  expect(treeMultiset.getHeight()).toBe(3);
123
123
 
124
- const removed5 = treeMultiset.delete(5, true);
124
+ const removed5 = treeMultiset.delete(5, undefined, true);
125
125
  expect(removed5 instanceof Array);
126
126
  expect(removed5[0]);
127
127
  expect(removed5[0].deleted);
@@ -130,7 +130,7 @@ describe('TreeMultiset operations test', () => {
130
130
  expect(treeMultiset.isAVLBalanced()).toBe(true);
131
131
  expect(treeMultiset.getHeight()).toBe(3);
132
132
 
133
- const removed13 = treeMultiset.delete(13, true);
133
+ const removed13 = treeMultiset.delete(13, undefined, true);
134
134
  expect(removed13 instanceof Array);
135
135
  expect(removed13[0]);
136
136
  expect(removed13[0].deleted);
@@ -138,7 +138,7 @@ describe('TreeMultiset operations test', () => {
138
138
  expect(treeMultiset.isAVLBalanced()).toBe(true);
139
139
  expect(treeMultiset.getHeight()).toBe(3);
140
140
 
141
- const removed3 = treeMultiset.delete(3, true);
141
+ const removed3 = treeMultiset.delete(3, undefined, true);
142
142
  expect(removed3 instanceof Array);
143
143
  expect(removed3[0]);
144
144
  expect(removed3[0].deleted);
@@ -146,7 +146,7 @@ describe('TreeMultiset operations test', () => {
146
146
  expect(treeMultiset.isAVLBalanced()).toBe(true);
147
147
  expect(treeMultiset.getHeight()).toBe(3);
148
148
 
149
- const removed8 = treeMultiset.delete(8, true);
149
+ const removed8 = treeMultiset.delete(8, undefined, true);
150
150
  expect(removed8 instanceof Array);
151
151
  expect(removed8[0]);
152
152
  expect(removed8[0].deleted);
@@ -154,17 +154,17 @@ describe('TreeMultiset operations test', () => {
154
154
  expect(treeMultiset.isAVLBalanced()).toBe(true);
155
155
  expect(treeMultiset.getHeight()).toBe(3);
156
156
 
157
- const removed6 = treeMultiset.delete(6, true);
157
+ const removed6 = treeMultiset.delete(6, undefined, true);
158
158
  expect(removed6 instanceof Array);
159
159
  expect(removed6[0]);
160
160
  expect(removed6[0].deleted);
161
161
  if (removed6[0].deleted) expect(removed6[0].deleted.key).toBe(6);
162
- expect(treeMultiset.delete(6, true).length).toBe(0);
162
+ expect(treeMultiset.delete(6, undefined, true).length).toBe(0);
163
163
  expect(treeMultiset.isAVLBalanced()).toBe(true);
164
164
 
165
165
  expect(treeMultiset.getHeight()).toBe(2);
166
166
 
167
- const removed7 = treeMultiset.delete(7, true);
167
+ const removed7 = treeMultiset.delete(7, undefined, true);
168
168
  expect(removed7 instanceof Array);
169
169
  expect(removed7[0]);
170
170
  expect(removed7[0].deleted);
@@ -172,7 +172,7 @@ describe('TreeMultiset operations test', () => {
172
172
  expect(treeMultiset.isAVLBalanced()).toBe(true);
173
173
  expect(treeMultiset.getHeight()).toBe(2);
174
174
 
175
- const removed9 = treeMultiset.delete(9, true);
175
+ const removed9 = treeMultiset.delete(9, undefined, true);
176
176
  expect(removed9 instanceof Array);
177
177
  expect(removed9[0]);
178
178
  expect(removed9[0].deleted);
@@ -180,7 +180,7 @@ describe('TreeMultiset operations test', () => {
180
180
  expect(treeMultiset.isAVLBalanced()).toBe(true);
181
181
  expect(treeMultiset.getHeight()).toBe(2);
182
182
 
183
- const removed14 = treeMultiset.delete(14, true);
183
+ const removed14 = treeMultiset.delete(14, undefined, true);
184
184
  expect(removed14 instanceof Array);
185
185
  expect(removed14[0]);
186
186
  expect(removed14[0].deleted);
@@ -206,7 +206,7 @@ describe('TreeMultiset operations test', () => {
206
206
  });
207
207
 
208
208
  it('should perform various operations on a Binary Search Tree with object values', () => {
209
- const objTreeMultiset = new TreeMultiset<TreeMultisetNode<{key: number; keyA: number}>>();
209
+ const objTreeMultiset = new TreeMultiset<{key: number; keyA: number}>();
210
210
  expect(objTreeMultiset).toBeInstanceOf(TreeMultiset);
211
211
  objTreeMultiset.add(11, {key: 11, keyA: 11});
212
212
  objTreeMultiset.add(3, {key: 3, keyA: 3});