linked-list-typed 1.39.0 → 1.39.2

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 (41) hide show
  1. package/dist/data-structures/binary-tree/avl-tree.d.ts +2 -2
  2. package/dist/data-structures/binary-tree/binary-tree.d.ts +96 -32
  3. package/dist/data-structures/binary-tree/binary-tree.js +46 -8
  4. package/dist/data-structures/binary-tree/bst.d.ts +6 -6
  5. package/dist/data-structures/binary-tree/bst.js +2 -2
  6. package/dist/data-structures/binary-tree/tree-multiset.d.ts +2 -2
  7. package/dist/data-structures/graph/abstract-graph.d.ts +1 -1
  8. package/dist/data-structures/graph/abstract-graph.js +1 -1
  9. package/dist/data-structures/graph/map-graph.d.ts +1 -1
  10. package/dist/data-structures/graph/map-graph.js +1 -1
  11. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
  12. package/dist/data-structures/linked-list/doubly-linked-list.js +59 -49
  13. package/dist/data-structures/linked-list/singly-linked-list.d.ts +75 -7
  14. package/dist/data-structures/linked-list/singly-linked-list.js +110 -9
  15. package/dist/data-structures/matrix/matrix2d.d.ts +1 -2
  16. package/dist/data-structures/matrix/matrix2d.js +3 -7
  17. package/dist/data-structures/matrix/vector2d.d.ts +0 -1
  18. package/dist/data-structures/matrix/vector2d.js +0 -1
  19. package/dist/data-structures/queue/deque.d.ts +20 -20
  20. package/dist/data-structures/queue/deque.js +22 -22
  21. package/dist/data-structures/queue/queue.d.ts +3 -3
  22. package/dist/data-structures/queue/queue.js +3 -3
  23. package/dist/interfaces/binary-tree.d.ts +2 -2
  24. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -2
  25. package/dist/types/helpers.d.ts +1 -4
  26. package/package.json +2 -2
  27. package/src/data-structures/binary-tree/avl-tree.ts +2 -2
  28. package/src/data-structures/binary-tree/binary-tree.ts +76 -90
  29. package/src/data-structures/binary-tree/bst.ts +9 -16
  30. package/src/data-structures/binary-tree/tree-multiset.ts +2 -2
  31. package/src/data-structures/graph/abstract-graph.ts +1 -1
  32. package/src/data-structures/graph/map-graph.ts +2 -2
  33. package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
  34. package/src/data-structures/linked-list/singly-linked-list.ts +118 -13
  35. package/src/data-structures/matrix/matrix2d.ts +1 -3
  36. package/src/data-structures/matrix/vector2d.ts +0 -2
  37. package/src/data-structures/queue/deque.ts +22 -22
  38. package/src/data-structures/queue/queue.ts +3 -3
  39. package/src/interfaces/binary-tree.ts +2 -2
  40. package/src/types/data-structures/binary-tree/binary-tree.ts +0 -4
  41. package/src/types/helpers.ts +1 -7
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { BST, BSTNode } from './bst';
9
9
  import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeKey } from '../../types';
10
- import { MapCallback } from '../../types';
10
+ import { OneParamCallback } from '../../types';
11
11
  import { IBinaryTree } from '../../interfaces';
12
12
  export declare class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNested<V>> extends BSTNode<V, N> {
13
13
  height: number;
@@ -53,7 +53,7 @@ export declare class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<
53
53
  * `this._defaultCallbackByKey`
54
54
  * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
55
55
  */
56
- delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeletedResult<N>[];
56
+ delete<C extends OneParamCallback<N>>(identifier: ReturnType<C>, callback?: C): BinaryTreeDeletedResult<N>[];
57
57
  /**
58
58
  * The function swaps the key, value, and height properties between two nodes in a binary tree.
59
59
  * @param {N} srcNode - The `srcNode` parameter represents the source node that needs to be swapped
@@ -5,8 +5,8 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BFSCallback, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, MapCallback } from '../../types';
9
- import { BinaryTreeDeletedResult, DefaultMapCallback, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
8
+ import type { BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback } from '../../types';
9
+ import { BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  /**
12
12
  * Represents a node in a binary tree.
@@ -132,8 +132,21 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
132
132
  * @returns The method is returning a boolean value.
133
133
  */
134
134
  refill(keysOrNodes: (BinaryTreeNodeKey | null)[] | (N | null)[], data?: Array<V>): boolean;
135
- delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N): BinaryTreeDeletedResult<N>[];
136
- delete<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): BinaryTreeDeletedResult<N>[];
135
+ /**
136
+ * The `delete` function removes a node from a binary search tree and returns the deleted node along
137
+ * with the parent node that needs to be balanced.
138
+ * a key (`BinaryTreeNodeKey`). If it is a key, the function will find the corresponding node in the
139
+ * binary tree.
140
+ * @returns an array of `BinaryTreeDeletedResult<N>` objects.
141
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
142
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
143
+ * searching for. It can be a specific key value or any other property of the node.
144
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
145
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
146
+ * included in the result. The `callback` parameter has a default value of
147
+ * `this._defaultCallbackByKey`, which
148
+ */
149
+ delete<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C): BinaryTreeDeletedResult<N>[];
137
150
  /**
138
151
  * The function `getDepth` calculates the depth of a given node in a binary tree relative to a
139
152
  * specified root node.
@@ -179,21 +192,62 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
179
192
  * @returns The method is returning a boolean value.
180
193
  */
181
194
  isPerfectlyBalanced(beginRoot?: N | null): boolean;
182
- getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N[];
183
- getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N[];
184
- getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, onlyOne: boolean): N[];
185
- getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean): N[];
186
- getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean, beginRoot: N | null): N[];
187
- getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, onlyOne: boolean, beginRoot: N | null, iterationType: IterationType): N[];
188
- has<C extends MapCallback<N>>(identifier: ReturnType<C> | N): boolean;
189
- has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): boolean;
190
- has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): boolean;
191
- has<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): boolean;
192
- get<C extends MapCallback<N>>(identifier: ReturnType<C> | N): N | null;
193
- get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C): N | null;
194
- get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, beginRoot: N | null): N | null;
195
- get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null): N | null;
196
- get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback: C, beginRoot: N | null, iterationType: IterationType): N | null;
195
+ /**
196
+ * The function `getNodes` returns an array of nodes that match a given node property, using either
197
+ * recursive or iterative traversal.
198
+ * @param {ReturnType<C>} identifier - The `identifier` parameter is either a
199
+ * `BinaryTreeNodeKey` or a generic type `N`. It represents the property of the node that we are
200
+ * searching for. It can be a specific key value or any other property of the node.
201
+ * @param callback - The `callback` parameter is a function that takes a node as input and returns a
202
+ * value. This value is compared with the `identifier` parameter to determine if the node should be
203
+ * included in the result. The `callback` parameter has a default value of
204
+ * `this._defaultCallbackByKey`, which
205
+ * @param [onlyOne=false] - A boolean value indicating whether to stop searching after finding the
206
+ * first node that matches the identifier. If set to true, the function will return an array with
207
+ * only one element (or an empty array if no matching node is found). If set to false (default), the
208
+ * function will continue searching for all
209
+ * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node from which the
210
+ * traversal of the binary tree will begin. It is optional and defaults to the root of the binary
211
+ * tree.
212
+ * @param iterationType - The `iterationType` parameter determines the type of iteration used to
213
+ * traverse the binary tree. It can have two possible values:
214
+ * @returns The function `getNodes` returns an array of nodes (`N[]`).
215
+ */
216
+ getNodes<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
217
+ /**
218
+ * The function checks if a binary tree has a node with a given property or key.
219
+ * @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
220
+ * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or a
221
+ * generic type `N`.
222
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
223
+ * matches the desired criteria. It takes a node as input and returns a boolean value indicating
224
+ * whether the node matches the criteria or not. The default callback function
225
+ * `this._defaultCallbackByKey` is used if no callback function is
226
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
227
+ * the node from which the search should begin. By default, it is set to `this.root`, which means the
228
+ * search will start from the root node of the binary tree. However, you can provide a different node
229
+ * as
230
+ * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
231
+ * performed when searching for nodes in the binary tree. It can have one of the following values:
232
+ * @returns a boolean value.
233
+ */
234
+ has<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): boolean;
235
+ /**
236
+ * The function `get` returns the first node in a binary tree that matches the given property or key.
237
+ * @param {BinaryTreeNodeKey | N} identifier - The `identifier` parameter is the key or value of
238
+ * the node that you want to find in the binary tree. It can be either a `BinaryTreeNodeKey` or `N`
239
+ * type.
240
+ * @param callback - The `callback` parameter is a function that is used to determine whether a node
241
+ * matches the desired criteria. It takes a node as input and returns a boolean value indicating
242
+ * whether the node matches the criteria or not. The default callback function
243
+ * (`this._defaultCallbackByKey`) is used if no callback function is
244
+ * @param beginRoot - The `beginRoot` parameter is the starting point for the search. It specifies
245
+ * the root node from which the search should begin.
246
+ * @param iterationType - The `iterationType` parameter specifies the type of iteration to be
247
+ * performed when searching for a node in the binary tree. It can have one of the following values:
248
+ * @returns either the found node (of type N) or null if no node is found.
249
+ */
250
+ get<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
197
251
  /**
198
252
  * The function `getPathToRoot` returns an array of nodes starting from a given node and traversing
199
253
  * up to the root node, with the option to reverse the order of the nodes.
@@ -238,7 +292,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
238
292
  * possible values:
239
293
  * @returns The function `isSubtreeBST` returns a boolean value.
240
294
  */
241
- isSubtreeBST(beginRoot: N, iterationType?: IterationType): boolean;
295
+ isSubtreeBST(beginRoot: N | null, iterationType?: IterationType): boolean;
242
296
  /**
243
297
  * The function checks if a binary tree is a binary search tree.
244
298
  * @param iterationType - The parameter "iterationType" is used to specify the type of iteration to
@@ -260,9 +314,9 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
260
314
  * start from the root of the tree.
261
315
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
262
316
  * performed on the binary tree. It can have two possible values:
263
- * @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
317
+ * @returns The function `subTreeTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
264
318
  */
265
- subTreeTraverse<C extends MapCallback<N>>(callback?: C, beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
319
+ subTreeTraverse<C extends OneParamCallback<N>>(callback?: C, beginRoot?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
266
320
  /**
267
321
  * The `dfs` function performs a depth-first search traversal on a binary tree, executing a callback
268
322
  * function on each node according to a specified order pattern.
@@ -276,23 +330,23 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
276
330
  * is `null`, an empty array will be returned.
277
331
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
278
332
  * iteration used in the depth-first search algorithm. It can have two possible values:
279
- * @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
333
+ * @returns The function `dfs` returns an array of `ReturnType<OneParamCallback<N>>` values.
280
334
  */
281
- dfs<C extends MapCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
335
+ dfs<C extends OneParamCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
282
336
  /**
283
337
  * The bfs function performs a breadth-first search traversal on a binary tree, executing a callback
284
338
  * function on each node.
285
339
  * @param callback - The `callback` parameter is a function that will be called for each node in the
286
340
  * breadth-first search. It takes a node of type `N` as its argument and returns a value of type
287
- * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
341
+ * `ReturnType<OneParamCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
288
342
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
289
343
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
290
344
  * will not be performed and an empty array will be returned.
291
345
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
292
346
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
293
- * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
347
+ * @returns The function `bfs` returns an array of `ReturnType<OneParamCallback<N>>[]`.
294
348
  */
295
- bfs<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
349
+ bfs<C extends OneParamCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[];
296
350
  /**
297
351
  * The `listLevels` function takes a binary tree node and a callback function, and returns an array
298
352
  * of arrays representing the levels of the tree.
@@ -308,7 +362,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
308
362
  * level in a binary tree. Each inner array contains the return type of the provided callback
309
363
  * function `C` applied to the nodes at that level.
310
364
  */
311
- listLevels<C extends BFSCallback<N> = BFSCallback<N, BinaryTreeNodeKey>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[][];
365
+ listLevels<C extends OneParamCallback<N>>(callback?: C, beginRoot?: N | null, iterationType?: IterationType): ReturnType<C>[][];
312
366
  /**
313
367
  * The function returns the predecessor node of a given node in a binary tree.
314
368
  * @param {N} node - The parameter "node" represents a node in a binary tree.
@@ -319,7 +373,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
319
373
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
320
374
  * algorithm and returns an array of values obtained by applying a callback function to each node.
321
375
  * @param callback - The `callback` parameter is a function that will be called on each node in the
322
- * tree. It takes a node of type `N` as input and returns a value of type `MapCallbackReturn<N>`. The
376
+ * tree. It takes a node of type `N` as input and returns a value of type `ReturnType<OneParamCallback<N>>`. The
323
377
  * default value for this parameter is `this._defaultCallbackByKey`.
324
378
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
325
379
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
@@ -327,9 +381,19 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
327
381
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
328
382
  * traversal. It specifies the root node of the tree from which the traversal should begin. If
329
383
  * `beginRoot` is `null`, an empty array will be returned.
330
- * @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
384
+ * @returns The `morris` function returns an array of `ReturnType<OneParamCallback<N>>` values.
385
+ */
386
+ morris<C extends OneParamCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
387
+ /**
388
+ * The above function is an iterator for a binary tree that can be used to traverse the tree in
389
+ * either an iterative or recursive manner.
390
+ * @param node - The `node` parameter represents the current node in the binary tree from which the
391
+ * iteration starts. It is an optional parameter with a default value of `this.root`, which means
392
+ * that if no node is provided, the iteration will start from the root of the binary tree.
393
+ * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
394
+ * binary tree nodes in a specific order.
331
395
  */
332
- morris<C extends MapCallback<N>>(callback?: C, pattern?: DFSOrderPattern, beginRoot?: N | null): ReturnType<C>[];
396
+ [Symbol.iterator](node?: N | null): Generator<BinaryTreeNodeKey, void, undefined>;
333
397
  /**
334
398
  * Swap the data of two nodes in the binary tree.
335
399
  * @param {N} srcNode - The source node to swap.
@@ -344,7 +408,7 @@ export declare class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = Binary
344
408
  * the tree's structure should be restored to its original state to maintain the tree's integrity.
345
409
  * This is because the purpose of the Morris algorithm is to save space rather than permanently alter the tree's shape.
346
410
  */
347
- protected _defaultCallbackByKey: DefaultMapCallback<N>;
411
+ protected _defaultCallbackByKey: OneParamCallback<N, BinaryTreeNodeKey>;
348
412
  /**
349
413
  * The function `_addTo` adds a new node to a binary tree if there is an available position.
350
414
  * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to
@@ -277,8 +277,8 @@ class BinaryTree {
277
277
  let needBalanced = null, orgCurrent = curr;
278
278
  if (!curr.left) {
279
279
  if (!parent) {
280
- if (curr.right !== undefined)
281
- this._setRoot(curr.right);
280
+ // Handle the case when there's only one root node
281
+ this._setRoot(null);
282
282
  }
283
283
  else {
284
284
  const { familyPosition: fp } = curr;
@@ -703,7 +703,7 @@ class BinaryTree {
703
703
  * start from the root of the tree.
704
704
  * @param iterationType - The `iterationType` parameter determines the type of traversal to be
705
705
  * performed on the binary tree. It can have two possible values:
706
- * @returns The function `subTreeTraverse` returns an array of `MapCallbackReturn<N>`.
706
+ * @returns The function `subTreeTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
707
707
  */
708
708
  subTreeTraverse(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
709
709
  if (typeof beginRoot === 'number')
@@ -743,7 +743,7 @@ class BinaryTree {
743
743
  * is `null`, an empty array will be returned.
744
744
  * @param {IterationType} iterationType - The `iterationType` parameter determines the type of
745
745
  * iteration used in the depth-first search algorithm. It can have two possible values:
746
- * @returns The function `dfs` returns an array of `MapCallbackReturn<N>` values.
746
+ * @returns The function `dfs` returns an array of `ReturnType<OneParamCallback<N>>` values.
747
747
  */
748
748
  dfs(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root, iterationType = types_1.IterationType.ITERATIVE) {
749
749
  if (!beginRoot)
@@ -820,13 +820,13 @@ class BinaryTree {
820
820
  * function on each node.
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
- * `BFSCallbackReturn<N>`. The default value for this parameter is `this._defaultCallbackByKey
823
+ * `ReturnType<OneParamCallback<N>>`. The default value for this parameter is `this._defaultCallbackByKey
824
824
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the breadth-first
825
825
  * search. It determines from which node the search will begin. If `beginRoot` is `null`, the search
826
826
  * will not be performed and an empty array will be returned.
827
827
  * @param iterationType - The `iterationType` parameter determines the type of iteration to be used
828
828
  * in the breadth-first search (BFS) algorithm. It can have two possible values:
829
- * @returns The function `bfs` returns an array of `BFSCallbackReturn<N>[]`.
829
+ * @returns The function `bfs` returns an array of `ReturnType<OneParamCallback<N>>[]`.
830
830
  */
831
831
  bfs(callback = this._defaultCallbackByKey, beginRoot = this.root, iterationType = this.iterationType) {
832
832
  if (!beginRoot)
@@ -934,7 +934,7 @@ class BinaryTree {
934
934
  * The `morris` function performs a depth-first traversal of a binary tree using the Morris traversal
935
935
  * algorithm and returns an array of values obtained by applying a callback function to each node.
936
936
  * @param callback - The `callback` parameter is a function that will be called on each node in the
937
- * tree. It takes a node of type `N` as input and returns a value of type `MapCallbackReturn<N>`. The
937
+ * tree. It takes a node of type `N` as input and returns a value of type `ReturnType<OneParamCallback<N>>`. The
938
938
  * default value for this parameter is `this._defaultCallbackByKey`.
939
939
  * @param {DFSOrderPattern} [pattern=in] - The `pattern` parameter in the `morris` function
940
940
  * determines the order in which the nodes of a binary tree are traversed. It can have one of the
@@ -942,7 +942,7 @@ class BinaryTree {
942
942
  * @param {N | null} beginRoot - The `beginRoot` parameter is the starting node for the Morris
943
943
  * traversal. It specifies the root node of the tree from which the traversal should begin. If
944
944
  * `beginRoot` is `null`, an empty array will be returned.
945
- * @returns The `morris` function returns an array of `MapCallbackReturn<N>` values.
945
+ * @returns The `morris` function returns an array of `ReturnType<OneParamCallback<N>>` values.
946
946
  */
947
947
  morris(callback = this._defaultCallbackByKey, pattern = 'in', beginRoot = this.root) {
948
948
  if (beginRoot === null)
@@ -1028,6 +1028,44 @@ class BinaryTree {
1028
1028
  }
1029
1029
  return ans;
1030
1030
  }
1031
+ /**
1032
+ * The above function is an iterator for a binary tree that can be used to traverse the tree in
1033
+ * either an iterative or recursive manner.
1034
+ * @param node - The `node` parameter represents the current node in the binary tree from which the
1035
+ * iteration starts. It is an optional parameter with a default value of `this.root`, which means
1036
+ * that if no node is provided, the iteration will start from the root of the binary tree.
1037
+ * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1038
+ * binary tree nodes in a specific order.
1039
+ */
1040
+ *[Symbol.iterator](node = this.root) {
1041
+ if (!node) {
1042
+ return;
1043
+ }
1044
+ if (this.iterationType === types_1.IterationType.ITERATIVE) {
1045
+ const stack = [];
1046
+ let current = node;
1047
+ while (current || stack.length > 0) {
1048
+ while (current) {
1049
+ stack.push(current);
1050
+ current = current.left;
1051
+ }
1052
+ current = stack.pop();
1053
+ if (current)
1054
+ yield current.key;
1055
+ if (current)
1056
+ current = current.right;
1057
+ }
1058
+ }
1059
+ else {
1060
+ if (node.left) {
1061
+ yield* this[Symbol.iterator](node.left);
1062
+ }
1063
+ yield node.key;
1064
+ if (node.right) {
1065
+ yield* this[Symbol.iterator](node.right);
1066
+ }
1067
+ }
1068
+ }
1031
1069
  /**
1032
1070
  * Swap the data of two nodes in the binary tree.
1033
1071
  * @param {N} srcNode - The source node to swap.
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, MapCallback } from '../../types';
8
+ import type { BinaryTreeNodeKey, BSTComparator, BSTNodeNested, BSTOptions, OneParamCallback } from '../../types';
9
9
  import { CP, IterationType } from '../../types';
10
10
  import { BinaryTree, BinaryTreeNode } from './binary-tree';
11
11
  import { IBinaryTree } from '../../interfaces';
@@ -59,7 +59,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
59
59
  * callback.
60
60
  * @param {ReturnType<C> | N} identifier - The `nodeProperty` parameter is used to specify the
61
61
  * property of the binary tree node that you want to search for. It can be either a specific key
62
- * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
62
+ * value (`BinaryTreeNodeKey`) or a custom callback function (`OneParamCallback<N>`) that determines
63
63
  * whether a node matches the desired property.
64
64
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
65
65
  * matches the desired property. It takes a node as input and returns a boolean value indicating
@@ -72,7 +72,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
72
72
  * @returns either the first node that matches the given nodeProperty and callback, or null if no
73
73
  * matching node is found.
74
74
  */
75
- get<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
75
+ get<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, beginRoot?: N | null, iterationType?: IterationType): N | null;
76
76
  /**
77
77
  * The function `lastKey` returns the key of the rightmost node if the comparison result is less
78
78
  * than, the key of the leftmost node if the comparison result is greater than, and the key of the
@@ -110,7 +110,7 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
110
110
  * traverse the binary tree. It can have one of the following values:
111
111
  * @returns an array of nodes (N[]).
112
112
  */
113
- getNodes<C extends MapCallback<N>>(identifier: ReturnType<C> | N, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
113
+ getNodes<C extends OneParamCallback<N>>(identifier: ReturnType<C> | null, callback?: C, onlyOne?: boolean, beginRoot?: N | null, iterationType?: IterationType): N[];
114
114
  /**
115
115
  * The `lesserOrGreaterTraverse` function traverses a binary tree and applies a callback function to
116
116
  * nodes that have a key value lesser or greater than a target key value.
@@ -126,9 +126,9 @@ export declare class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNe
126
126
  * (`BinaryTreeNodeKey`), or `null` to
127
127
  * @param iterationType - The `iterationType` parameter determines whether the traversal should be
128
128
  * done recursively or iteratively. It can have two possible values:
129
- * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
129
+ * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
130
130
  */
131
- lesserOrGreaterTraverse<C extends MapCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
131
+ lesserOrGreaterTraverse<C extends OneParamCallback<N>>(callback?: C, lesserOrGreater?: CP, targetNode?: BinaryTreeNodeKey | N | null, iterationType?: IterationType): ReturnType<C>[];
132
132
  /**
133
133
  * Balancing Adjustment:
134
134
  * Perfectly Balanced Binary Tree: Since the balance of a perfectly balanced binary tree is already fixed, no additional balancing adjustment is needed. Any insertion or deletion operation will disrupt the perfect balance, often requiring a complete reconstruction of the tree.
@@ -210,7 +210,7 @@ class BST extends binary_tree_1.BinaryTree {
210
210
  * callback.
211
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
- * value (`BinaryTreeNodeKey`) or a custom callback function (`MapCallback<N>`) that determines
213
+ * value (`BinaryTreeNodeKey`) or a custom callback function (`OneParamCallback<N>`) that determines
214
214
  * whether a node matches the desired property.
215
215
  * @param callback - The `callback` parameter is a function that is used to determine whether a node
216
216
  * matches the desired property. It takes a node as input and returns a boolean value indicating
@@ -343,7 +343,7 @@ class BST extends binary_tree_1.BinaryTree {
343
343
  * (`BinaryTreeNodeKey`), or `null` to
344
344
  * @param iterationType - The `iterationType` parameter determines whether the traversal should be
345
345
  * done recursively or iteratively. It can have two possible values:
346
- * @returns The function `lesserOrGreaterTraverse` returns an array of `MapCallbackReturn<N>`.
346
+ * @returns The function `lesserOrGreaterTraverse` returns an array of `ReturnType<OneParamCallback<N>>`.
347
347
  */
348
348
  lesserOrGreaterTraverse(callback = this._defaultCallbackByKey, lesserOrGreater = types_1.CP.lt, targetNode = this.root, iterationType = this.iterationType) {
349
349
  if (typeof targetNode === 'number')
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
  import type { BinaryTreeNodeKey, TreeMultisetNodeNested, TreeMultisetOptions } from '../../types';
9
- import { BinaryTreeDeletedResult, IterationType, MapCallback } from '../../types';
9
+ import { BinaryTreeDeletedResult, IterationType, OneParamCallback } from '../../types';
10
10
  import { IBinaryTree } from '../../interfaces';
11
11
  import { AVLTree, AVLTreeNode } from './avl-tree';
12
12
  export declare class TreeMultisetNode<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNodeNested<V>> extends AVLTreeNode<V, N> {
@@ -105,7 +105,7 @@ export declare class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = Tr
105
105
  * decremented by 1 and
106
106
  * @returns The method `delete` returns an array of `BinaryTreeDeletedResult<N>` objects.
107
107
  */
108
- delete<C extends MapCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
108
+ delete<C extends OneParamCallback<N>>(identifier: ReturnType<C>, callback?: C, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[];
109
109
  /**
110
110
  * The clear() function clears the contents of a data structure and sets the count to zero.
111
111
  */
@@ -103,7 +103,7 @@ export declare abstract class AbstractGraph<V extends AbstractVertex<any> = Abst
103
103
  * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
104
104
  * were removed.
105
105
  */
106
- removeAllVertices(vertices: V[] | VertexKey[]): boolean;
106
+ removeManyVertices(vertices: V[] | VertexKey[]): boolean;
107
107
  /**
108
108
  * The function checks if there is an edge between two vertices and returns a boolean value indicating the result.
109
109
  * @param {VertexKey | V} v1 - The parameter v1 can be either a VertexKey or a V. A VertexKey represents the unique
@@ -133,7 +133,7 @@ class AbstractGraph {
133
133
  * @returns a boolean value. It returns true if at least one vertex was successfully removed, and false if no vertices
134
134
  * were removed.
135
135
  */
136
- removeAllVertices(vertices) {
136
+ removeManyVertices(vertices) {
137
137
  const removed = [];
138
138
  for (const v of vertices) {
139
139
  removed.push(this.deleteVertex(v));
@@ -62,7 +62,7 @@ export declare class MapGraph<V extends MapVertex<V['val']> = MapVertex, E exten
62
62
  * @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
63
63
  * @returns The method is returning a new instance of the `MapVertex` class, casted as type `V`.
64
64
  */
65
- createVertex(key: VertexKey, val?: V['val'], lat?: number, long?: number): V;
65
+ createVertex(key: VertexKey, lat?: number, long?: number, val?: V['val']): V;
66
66
  /**
67
67
  * The function creates a new instance of a MapEdge with the given source, destination, weight, and value.
68
68
  * @param {VertexKey} src - The source vertex ID of the edge. It represents the starting point of the edge.
@@ -89,7 +89,7 @@ class MapGraph extends directed_graph_1.DirectedGraph {
89
89
  * @param {number} long - The `long` parameter represents the longitude coordinate of the vertex.
90
90
  * @returns The method is returning a new instance of the `MapVertex` class, casted as type `V`.
91
91
  */
92
- createVertex(key, val, lat = this.origin[0], long = this.origin[1]) {
92
+ createVertex(key, lat = this.origin[0], long = this.origin[1], val) {
93
93
  return new MapVertex(key, lat, long, val);
94
94
  }
95
95
  /**
@@ -60,11 +60,11 @@ export declare class DoublyLinkedList<E = any> {
60
60
  */
61
61
  pop(): E | undefined;
62
62
  /**
63
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
63
+ * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
64
64
  * @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
65
65
  * list is empty, it returns null.
66
66
  */
67
- pollLast(): E | undefined;
67
+ popLast(): E | undefined;
68
68
  /**
69
69
  * The `shift()` function removes and returns the value of the first node in a doubly linked list.
70
70
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
@@ -72,11 +72,11 @@ export declare class DoublyLinkedList<E = any> {
72
72
  */
73
73
  shift(): E | undefined;
74
74
  /**
75
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
75
+ * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
76
76
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
77
77
  * list.
78
78
  */
79
- pollFirst(): E | undefined;
79
+ popFirst(): E | undefined;
80
80
  /**
81
81
  * The unshift function adds a new node with the given value to the beginning of a doubly linked list.
82
82
  * @param {E} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
@@ -90,15 +90,15 @@ export declare class DoublyLinkedList<E = any> {
90
90
  */
91
91
  addFirst(val: E): void;
92
92
  /**
93
- * The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty.
94
- * @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
93
+ * The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
94
+ * @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
95
95
  */
96
- peekFirst(): E | undefined;
96
+ getFirst(): E | undefined;
97
97
  /**
98
- * The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty.
99
- * @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
98
+ * The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
99
+ * @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
100
100
  */
101
- peekLast(): E | undefined;
101
+ getLast(): E | undefined;
102
102
  /**
103
103
  * The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
104
104
  * @param {number} index - The index parameter is a number that represents the position of the element we want to
@@ -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 | null): DoublyLinkedListNode<E> | null;
126
+ getNode(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
@@ -134,6 +134,17 @@ export declare class DoublyLinkedList<E = any> {
134
134
  * if the index is out of bounds.
135
135
  */
136
136
  insertAt(index: number, val: E): boolean;
137
+ /**
138
+ * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
139
+ * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
140
+ * before which the new value will be inserted. It can be either the value of the existing node or the existing node
141
+ * itself.
142
+ * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
143
+ * list.
144
+ * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
145
+ * insertion fails.
146
+ */
147
+ insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
137
148
  /**
138
149
  * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
139
150
  * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
@@ -142,8 +153,14 @@ export declare class DoublyLinkedList<E = any> {
142
153
  * bounds.
143
154
  */
144
155
  deleteAt(index: number): E | undefined;
145
- delete(valOrNode: E): boolean;
146
- delete(valOrNode: DoublyLinkedListNode<E>): boolean;
156
+ /**
157
+ * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
158
+ * @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
159
+ * a `DoublyLinkedListNode<E>` object.
160
+ * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
161
+ * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
162
+ */
163
+ delete(valOrNode: E | DoublyLinkedListNode<E> | null): boolean;
147
164
  /**
148
165
  * The `toArray` function converts a linked list into an array.
149
166
  * @returns The `toArray()` method is returning an array of type `E[]`.
@@ -175,19 +192,19 @@ export declare class DoublyLinkedList<E = any> {
175
192
  */
176
193
  indexOf(val: E): number;
177
194
  /**
178
- * The `findLast` function iterates through a linked list from the last node to the first node and returns the last
195
+ * The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
179
196
  * value that satisfies the given callback function, or null if no value satisfies the callback.
180
197
  * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
181
198
  * function is used to determine whether a given value satisfies a certain condition.
182
- * @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
199
+ * @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
183
200
  * the callback function. If no value satisfies the condition, it returns `null`.
184
201
  */
185
- findLast(callback: (val: E) => boolean): E | null;
202
+ findBackward(callback: (val: E) => boolean): E | null;
186
203
  /**
187
- * The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
188
- * @returns The `toArrayReverse()` function returns an array of type `E[]`.
204
+ * The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
205
+ * @returns The `toArrayBackward()` function returns an array of type `E[]`.
189
206
  */
190
- toArrayReverse(): E[];
207
+ toArrayBackward(): E[];
191
208
  /**
192
209
  * The `reverse` function reverses the order of the elements in a doubly linked list.
193
210
  */
@@ -227,17 +244,18 @@ export declare class DoublyLinkedList<E = any> {
227
244
  * elements in the linked list.
228
245
  */
229
246
  reduce<U>(callback: (accumulator: U, val: E) => U, initialValue: U): U;
230
- insertAfter(existingValueOrNode: E, newValue: E): boolean;
231
- insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
232
247
  /**
233
- * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
248
+ * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
234
249
  * @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
250
+ * after which the new value will be inserted. It can be either the value of the existing node or the existing node
236
251
  * 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.
252
+ * @param {E} newValue - The value that you want to insert into the doubly linked list.
253
+ * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
254
+ * existing value or node is not found in the doubly linked list.
241
255
  */
242
- insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
256
+ insertAfter(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean;
257
+ /**
258
+ * The function returns an iterator that iterates over the values of a linked list.
259
+ */
260
+ [Symbol.iterator](): Generator<E, void, unknown>;
243
261
  }