linked-list-typed 1.38.4 → 1.38.6

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.
@@ -126,7 +126,7 @@ class BST extends binary_tree_1.BinaryTree {
126
126
  /**
127
127
  * The `addMany` function is used to efficiently add multiple nodes to a binary search tree while
128
128
  * maintaining balance.
129
- * @param {[BinaryTreeNodeKey | N, N['val']][]} arr - The `arr` parameter in the `addMany` function
129
+ * @param {[BinaryTreeNodeKey | N, N['val']][]} keysOrNodes - The `arr` parameter in the `addMany` function
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
@@ -208,7 +208,7 @@ class BST extends binary_tree_1.BinaryTree {
208
208
  /**
209
209
  * The function returns the first node in the binary tree that matches the given node property and
210
210
  * callback.
211
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter is used to specify the
211
+ * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
212
212
  * property of the binary tree node that you want to search for. It can be either a specific key
213
213
  * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
214
214
  * whether a node matches the desired property.
@@ -223,9 +223,9 @@ class BST extends binary_tree_1.BinaryTree {
223
223
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
224
224
  * matching node is found.
225
225
  */
226
- get(nodeProperty, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
226
+ get(identifier, callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
227
227
  var _a;
228
- return (_a = this.getNodes(nodeProperty, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
228
+ return (_a = this.getNodes(identifier, callback, true, beginRoot, iterationType)[0]) !== null && _a !== void 0 ? _a : null;
229
229
  }
230
230
  /**
231
231
  * The function `lastKey` returns the key of the rightmost node if the comparison result is less
@@ -254,7 +254,7 @@ class BST extends binary_tree_1.BinaryTree {
254
254
  /**
255
255
  * The function `getNodes` retrieves nodes from a binary tree based on a given node property or key,
256
256
  * using either recursive or iterative traversal.
257
- * @param {BinaryTreeNodeKey | N} nodeProperty - The `nodeProperty` parameter represents the property
257
+ * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter represents the property
258
258
  * of the binary tree node that you want to search for. It can be either a `BinaryTreeNodeKey` or a
259
259
  * generic type `N`.
260
260
  * @param callback - The `callback` parameter is a function that takes a node as input and returns a
@@ -272,14 +272,14 @@ class BST extends binary_tree_1.BinaryTree {
272
272
  * traverse the binary tree. It can have one of the following values:
273
273
  * @returns an array of nodes (N[]).
274
274
  */
275
- getNodes(nodeProperty, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
275
+ getNodes(identifier, callback = this._defaultCallbackByKey, onlyOne = false, beginRoot = this.root, iterationType = this.iterationType) {
276
276
  if (!beginRoot)
277
277
  return [];
278
278
  const ans = [];
279
279
  if (iterationType === types_1.IterationType.RECURSIVE) {
280
280
  const _traverse = (cur) => {
281
281
  const callbackResult = callback(cur);
282
- if (callbackResult === nodeProperty) {
282
+ if (callbackResult === identifier) {
283
283
  ans.push(cur);
284
284
  if (onlyOne)
285
285
  return;
@@ -288,9 +288,9 @@ class BST extends binary_tree_1.BinaryTree {
288
288
  return;
289
289
  // TODO potential bug
290
290
  if (callback === this._defaultCallbackByKey) {
291
- if (this._compare(cur.key, nodeProperty) === types_1.CP.gt)
291
+ if (this._compare(cur.key, identifier) === types_1.CP.gt)
292
292
  cur.left && _traverse(cur.left);
293
- if (this._compare(cur.key, nodeProperty) === types_1.CP.lt)
293
+ if (this._compare(cur.key, identifier) === types_1.CP.lt)
294
294
  cur.right && _traverse(cur.right);
295
295
  }
296
296
  else {
@@ -306,16 +306,16 @@ class BST extends binary_tree_1.BinaryTree {
306
306
  const cur = queue.shift();
307
307
  if (cur) {
308
308
  const callbackResult = callback(cur);
309
- if (callbackResult === nodeProperty) {
309
+ if (callbackResult === identifier) {
310
310
  ans.push(cur);
311
311
  if (onlyOne)
312
312
  return ans;
313
313
  }
314
314
  // TODO potential bug
315
315
  if (callback === this._defaultCallbackByKey) {
316
- if (this._compare(cur.key, nodeProperty) === types_1.CP.gt)
316
+ if (this._compare(cur.key, identifier) === types_1.CP.gt)
317
317
  cur.left && queue.push(cur.left);
318
- if (this._compare(cur.key, nodeProperty) === types_1.CP.lt)
318
+ if (this._compare(cur.key, identifier) === types_1.CP.lt)
319
319
  cur.right && queue.push(cur.right);
320
320
  }
321
321
  else {
@@ -337,7 +337,7 @@ class BST extends binary_tree_1.BinaryTree {
337
337
  * @param {CP} lesserOrGreater - The `lesserOrGreater` parameter is used to determine whether to
338
338
  * traverse nodes that are lesser than, greater than, or equal to the `targetNode`. It can take one
339
339
  * of the following values:
340
- * @param {N | BinaryTreeNodeKey | null} targetNode - The `targetNode` parameter in the
340
+ * @param {BinaryTreeNodeKey | N | null} targetNode - The `targetNode` parameter in the
341
341
  * `lesserOrGreaterTraverse` function is used to specify the node from which the traversal should
342
342
  * start. It can be either a reference to a specific node (`N`), the key of a node
343
343
  * (`BinaryTreeNodeKey`), or `null` to
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
9
- import { BinaryTreeDeletedResult, IterationType } from '../../types';
9
+ import { BinaryTreeDeletedResult, IterationType, MapCallback } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
12
  export declare class TreeMultisetNode<V = any, FAMILY extends TreeMultisetNode<V, FAMILY> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, FAMILY> {
@@ -92,16 +92,20 @@ export declare class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = Tree
92
92
  /**
93
93
  * The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
94
94
  * node along with the parent node that needs to be balanced.
95
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
96
- * (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
97
- * from the binary tree.
95
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
96
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
97
+ * searching for. It can be a specific key value or any other property of the node.
98
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
99
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
100
+ * included in the result. The `callback` parameter has a default value of
101
+ * `this._defaultCallbackByKey`
98
102
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
99
103
  * being deleted. If set to true, the count of the node will not be considered and the node will be
100
104
  * deleted regardless of its count. If set to false (default), the count of the node will be
101
105
  * decremented by 1 and
102
106
  * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
103
107
  */
104
- delete(nodeOrKey: N | BinaryTreeNodeKey, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
108
+ delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
105
109
  /**
106
110
  * The clear() function clears the contents of a data structure and sets the count to zero.
107
111
  */
@@ -245,20 +245,24 @@ class TreeMultiset extends avl_tree_1.AVLTree {
245
245
  /**
246
246
  * The `delete` function in a binary search tree deletes a node from the tree and returns the deleted
247
247
  * node along with the parent node that needs to be balanced.
248
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
249
- * (`N`) or a key value (`BinaryTreeNodeKey`). It represents the node or key that needs to be deleted
250
- * from the binary tree.
248
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
249
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
250
+ * searching for. It can be a specific key value or any other property of the node.
251
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
252
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
253
+ * included in the result. The `callback` parameter has a default value of
254
+ * `this._defaultCallbackByKey`
251
255
  * @param [ignoreCount=false] - A boolean flag indicating whether to ignore the count of the node
252
256
  * being deleted. If set to true, the count of the node will not be considered and the node will be
253
257
  * deleted regardless of its count. If set to false (default), the count of the node will be
254
258
  * decremented by 1 and
255
259
  * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
256
260
  */
257
- delete(nodeOrKey, ignoreCount = false) {
261
+ delete(identifier, callback = this._defaultCallbackByKey, ignoreCount = false) {
258
262
  const bstDeletedResult = [];
259
263
  if (!this.root)
260
264
  return bstDeletedResult;
261
- const curr = this.get(nodeOrKey);
265
+ const curr = this.get(identifier, callback);
262
266
  if (!curr)
263
267
  return bstDeletedResult;
264
268
  const parent = (curr === null || curr === void 0 ? void 0 : curr.parent) ? curr.parent : null;
@@ -123,7 +123,7 @@ export declare class DoublyLinkedList<E = any> {
123
123
  * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
124
124
  * is found in the linked list. If no such node is found, it returns `null`.
125
125
  */
126
- findNode(val: E): DoublyLinkedListNode<E> | null;
126
+ findNode(val: E | null): DoublyLinkedListNode<E> | null;
127
127
  /**
128
128
  * The `insert` function inserts a value at a specified index in a doubly linked list.
129
129
  * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
@@ -229,6 +229,15 @@ export declare class DoublyLinkedList<E = any> {
229
229
  reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
230
230
  insertAfter(existingValueOrNode: E, newValue: E): boolean;
231
231
  insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
232
- insertBefore(existingValueOrNode: E, newValue: E): boolean;
233
- insertBefore(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
232
+ /**
233
+ * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
234
+ * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
235
+ * before which the new value will be inserted. It can be either the value of the existing node or the existing node
236
+ * itself.
237
+ * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
238
+ * list.
239
+ * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
240
+ * insertion fails.
241
+ */
242
+ insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
234
243
  }
@@ -88,8 +88,14 @@ export declare class SinglyLinkedList<E = any> {
88
88
  * bounds.
89
89
  */
90
90
  deleteAt(index: number): E | undefined;
91
- delete(valueOrNode: E): boolean;
92
- delete(valueOrNode: SinglyLinkedListNode<E>): boolean;
91
+ /**
92
+ * The delete function removes a node with a specific value from a singly linked list.
93
+ * @param {E | SinglyLinkedListNode<E>} valueOrNode - The `valueOrNode` parameter can accept either a value of type `E`
94
+ * or a `SinglyLinkedListNode<E>` object.
95
+ * @returns The `delete` method returns a boolean value. It returns `true` if the value or node is found and
96
+ * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
97
+ */
98
+ delete(valueOrNode: E | SinglyLinkedListNode<E> | null | undefined): boolean;
93
99
  /**
94
100
  * The `insertAt` function inserts a value at a specified index in a singly linked list.
95
101
  * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
@@ -143,8 +149,15 @@ export declare class SinglyLinkedList<E = any> {
143
149
  * the specified value is found, the function returns `null`.
144
150
  */
145
151
  findNode(value: E): SinglyLinkedListNode<E> | null;
146
- insertBefore(existingValue: E, newValue: E): boolean;
147
- insertBefore(existingValue: SinglyLinkedListNode<E>, newValue: E): boolean;
152
+ /**
153
+ * The `insertBefore` function inserts a new value before an existing value in a singly linked list.
154
+ * @param {E | SinglyLinkedListNode<E>} existingValueOrNode - The existing value or node that you want to insert the
155
+ * new value before. It can be either the value itself or a node containing the value in the linked list.
156
+ * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the linked list.
157
+ * @returns The method `insertBefore` returns a boolean value. It returns `true` if the new value was successfully
158
+ * inserted before the existing value, and `false` otherwise.
159
+ */
160
+ insertBefore(existingValueOrNode: E | SinglyLinkedListNode<E>, newValue: E): boolean;
148
161
  insertAfter(existingValueOrNode: E, newValue: E): boolean;
149
162
  insertAfter(existingValueOrNode: SinglyLinkedListNode<E>, newValue: E): boolean;
150
163
  /**
@@ -202,6 +202,8 @@ class SinglyLinkedList {
202
202
  * successfully deleted from the linked list, and `false` if the value or node is not found in the linked list.
203
203
  */
204
204
  delete(valueOrNode) {
205
+ if (!valueOrNode)
206
+ return false;
205
207
  let value;
206
208
  if (valueOrNode instanceof SinglyLinkedListNode) {
207
209
  value = valueOrNode.val;
@@ -1,7 +1,7 @@
1
1
  import { BinaryTreeNode } from '../data-structures';
2
- import { BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../types';
2
+ import { BinaryTreeDeletedResult, BinaryTreeNodeKey, MapCallback } from '../types';
3
3
  export interface IBinaryTree<N extends BinaryTreeNode<N['val'], N>> {
4
4
  createNode(key: BinaryTreeNodeKey, val?: N['val']): N;
5
5
  add(keyOrNode: BinaryTreeNodeKey | N | null, val?: N['val']): N | null | undefined;
6
- delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[];
6
+ delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
7
7
  }
@@ -1,6 +1,8 @@
1
+ import { BinaryTreeNodeKey } from './data-structures';
1
2
  export type Comparator<T> = (a: T, b: T) => number;
2
3
  export type DFSOrderPattern = 'pre' | 'in' | 'post';
3
4
  export type MapCallback<N, D = any> = (node: N) => D;
5
+ export type DefaultMapCallback<N, D = BinaryTreeNodeKey> = (node: N) => D;
4
6
  export type MapCallbackReturn<N> = ReturnType<MapCallback<N>>;
5
7
  export declare enum CP {
6
8
  lt = "lt",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "linked-list-typed",
3
- "version": "1.38.4",
3
+ "version": "1.38.6",
4
4
  "description": "Linked List, Doubly Linked List, Singly Linked List. Javascript & Typescript Data Structure.",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -66,6 +66,6 @@
66
66
  "typescript": "^4.9.5"
67
67
  },
68
68
  "dependencies": {
69
- "data-structure-typed": "^1.38.4"
69
+ "data-structure-typed": "^1.38.6"
70
70
  }
71
71
  }
@@ -8,6 +8,7 @@
8
8
  import {BST, BSTNode} from './bst';
9
9
  import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey} from '../../types';
10
10
  import {IBinaryTree} from '../../interfaces';
11
+ import {MapCallback} from '../../types';
11
12
 
12
13
  export class AVLTreeNode<V = any, FAMILY extends AVLTreeNode<V, FAMILY> = AVLTreeNodeNested<V>> extends BSTNode<
13
14
  V,
@@ -64,12 +65,20 @@ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends B
64
65
  /**
65
66
  * The function overrides the delete method of a binary tree and balances the tree after deleting a
66
67
  * node if necessary.
67
- * @param {N | BinaryTreeNodeKey} nodeOrKey - The `nodeOrKey` parameter can be either a node object
68
- * (`N`) or a key value (`BinaryTreeNodeKey`).
68
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
69
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
70
+ * searching for. It can be a specific key value or any other property of the node.
71
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
72
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
73
+ * included in the result. The `callback` parameter has a default value of
74
+ * `this._defaultCallbackByKey`
69
75
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
70
76
  */
71
- override delete(nodeOrKey: N | BinaryTreeNodeKey): BinaryTreeDeletedResult<N>[] {
72
- const deletedResults = super.delete(nodeOrKey);
77
+ override delete<C extends MapCallback<N>>(
78
+ identifier: ReturnType<C>,
79
+ callback: C = this._defaultCallbackByKey as C
80
+ ): BinaryTreeDeletedResult<N>[] {
81
+ const deletedResults = super.delete(identifier, callback);
73
82
  for (const {needBalanced} of deletedResults) {
74
83
  if (needBalanced) {
75
84
  this._balancePath(needBalanced);