linked-list-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.
@@ -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 {