data-structure-typed 1.39.1 → 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 (92) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  3. package/dist/cjs/data-structures/binary-tree/binary-indexed-tree.js.map +1 -1
  4. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +11 -1
  5. package/dist/cjs/data-structures/binary-tree/binary-tree.js +38 -0
  6. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  7. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  8. package/dist/cjs/data-structures/binary-tree/rb-tree.js.map +1 -1
  9. package/dist/cjs/data-structures/binary-tree/tree-multiset.js.map +1 -1
  10. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  11. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  12. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  13. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  14. package/dist/cjs/data-structures/hash/tree-map.js.map +1 -1
  15. package/dist/cjs/data-structures/hash/tree-set.js.map +1 -1
  16. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  17. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  18. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  19. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
  20. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +59 -49
  21. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  22. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
  23. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +110 -9
  24. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  25. package/dist/cjs/data-structures/matrix/matrix.js.map +1 -1
  26. package/dist/cjs/data-structures/matrix/vector2d.js.map +1 -1
  27. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  28. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  29. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  30. package/dist/cjs/data-structures/queue/deque.d.ts +20 -20
  31. package/dist/cjs/data-structures/queue/deque.js +22 -22
  32. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  33. package/dist/cjs/data-structures/queue/queue.d.ts +3 -3
  34. package/dist/cjs/data-structures/queue/queue.js +3 -3
  35. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  36. package/dist/mjs/data-structures/binary-tree/binary-tree.d.ts +11 -1
  37. package/dist/mjs/data-structures/binary-tree/binary-tree.js +38 -0
  38. package/dist/mjs/data-structures/linked-list/doubly-linked-list.d.ts +46 -28
  39. package/dist/mjs/data-structures/linked-list/doubly-linked-list.js +59 -49
  40. package/dist/mjs/data-structures/linked-list/singly-linked-list.d.ts +75 -7
  41. package/dist/mjs/data-structures/linked-list/singly-linked-list.js +110 -9
  42. package/dist/mjs/data-structures/queue/deque.d.ts +20 -20
  43. package/dist/mjs/data-structures/queue/deque.js +22 -22
  44. package/dist/mjs/data-structures/queue/queue.d.ts +3 -3
  45. package/dist/mjs/data-structures/queue/queue.js +3 -3
  46. package/dist/umd/data-structure-typed.min.js +1 -1
  47. package/dist/umd/data-structure-typed.min.js.map +1 -1
  48. package/package.json +5 -5
  49. package/src/data-structures/binary-tree/avl-tree.ts +2 -3
  50. package/src/data-structures/binary-tree/binary-indexed-tree.ts +1 -1
  51. package/src/data-structures/binary-tree/binary-tree.ts +47 -5
  52. package/src/data-structures/binary-tree/bst.ts +1 -2
  53. package/src/data-structures/binary-tree/rb-tree.ts +1 -2
  54. package/src/data-structures/binary-tree/tree-multiset.ts +1 -2
  55. package/src/data-structures/graph/abstract-graph.ts +10 -11
  56. package/src/data-structures/graph/directed-graph.ts +1 -2
  57. package/src/data-structures/graph/undirected-graph.ts +4 -5
  58. package/src/data-structures/hash/hash-map.ts +1 -1
  59. package/src/data-structures/hash/tree-map.ts +2 -1
  60. package/src/data-structures/hash/tree-set.ts +2 -1
  61. package/src/data-structures/heap/heap.ts +2 -2
  62. package/src/data-structures/heap/max-heap.ts +1 -1
  63. package/src/data-structures/heap/min-heap.ts +1 -1
  64. package/src/data-structures/linked-list/doubly-linked-list.ts +62 -56
  65. package/src/data-structures/linked-list/singly-linked-list.ts +119 -14
  66. package/src/data-structures/matrix/matrix.ts +1 -1
  67. package/src/data-structures/matrix/vector2d.ts +2 -1
  68. package/src/data-structures/priority-queue/max-priority-queue.ts +1 -1
  69. package/src/data-structures/priority-queue/min-priority-queue.ts +1 -1
  70. package/src/data-structures/priority-queue/priority-queue.ts +1 -1
  71. package/src/data-structures/queue/deque.ts +27 -26
  72. package/src/data-structures/queue/queue.ts +4 -4
  73. package/src/types/data-structures/matrix/navigator.ts +1 -1
  74. package/src/types/utils/utils.ts +1 -1
  75. package/src/types/utils/validate-type.ts +2 -2
  76. package/test/integration/bst.test.ts +1 -1
  77. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +1 -1
  78. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +23 -3
  79. package/test/unit/data-structures/binary-tree/bst.test.ts +6 -6
  80. package/test/unit/data-structures/binary-tree/overall.test.ts +1 -1
  81. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +2 -2
  82. package/test/unit/data-structures/hash/coordinate-map.test.ts +20 -0
  83. package/test/unit/data-structures/hash/coordinate-set.test.ts +25 -0
  84. package/test/unit/data-structures/hash/hash-table.test.ts +3 -1
  85. package/test/unit/data-structures/heap/heap.test.ts +2 -2
  86. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +70 -7
  87. package/test/unit/data-structures/linked-list/linked-list.test.ts +2 -2
  88. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +60 -13
  89. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +2 -2
  90. package/test/unit/data-structures/queue/deque.test.ts +43 -43
  91. package/test/unit/data-structures/queue/queue.test.ts +7 -7
  92. package/test/utils/big-o.ts +1 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "data-structure-typed",
3
- "version": "1.39.1",
3
+ "version": "1.39.2",
4
4
  "description": "Data Structures of Javascript & TypeScript. Binary Tree, BST, Graph, Heap, Priority Queue, Linked List, Queue, Deque, Stack, AVL Tree, Tree Multiset, Trie, Directed Graph, Undirected Graph, Singly Linked List, Doubly Linked List, Max Heap, Max Priority Queue, Min Heap, Min Priority Queue.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/mjs/index.js",
@@ -61,17 +61,17 @@
61
61
  "@typescript-eslint/eslint-plugin": "^6.7.4",
62
62
  "@typescript-eslint/parser": "^6.7.4",
63
63
  "auto-changelog": "^2.4.0",
64
- "avl-tree-typed": "^1.39.0",
64
+ "avl-tree-typed": "^1.39.1",
65
65
  "benchmark": "^2.1.4",
66
- "binary-tree-typed": "^1.39.0",
67
- "bst-typed": "^1.39.0",
66
+ "binary-tree-typed": "^1.39.1",
67
+ "bst-typed": "^1.39.1",
68
68
  "dependency-cruiser": "^14.1.0",
69
69
  "eslint": "^8.50.0",
70
70
  "eslint-config-prettier": "^9.0.0",
71
71
  "eslint-import-resolver-alias": "^1.1.2",
72
72
  "eslint-import-resolver-typescript": "^3.6.1",
73
73
  "eslint-plugin-import": "^2.28.1",
74
- "heap-typed": "^1.39.0",
74
+ "heap-typed": "^1.39.1",
75
75
  "istanbul-badges-readme": "^1.8.5",
76
76
  "jest": "^29.7.0",
77
77
  "prettier": "^3.0.3",
@@ -21,8 +21,7 @@ export class AVLTreeNode<V = any, N extends AVLTreeNode<V, N> = AVLTreeNodeNeste
21
21
 
22
22
  export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTreeNodeNested<V>>>
23
23
  extends BST<V, N>
24
- implements IBinaryTree<V, N>
25
- {
24
+ implements IBinaryTree<V, N> {
26
25
  /**
27
26
  * This is a constructor function for an AVL tree data structure in TypeScript.
28
27
  * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
@@ -161,7 +160,7 @@ export class AVLTree<V = any, N extends AVLTreeNode<V, N> = AVLTreeNode<V, AVLTr
161
160
  // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
162
161
  switch (
163
162
  this._balanceFactor(A) // second O(1)
164
- ) {
163
+ ) {
165
164
  case -2:
166
165
  if (A && A.left) {
167
166
  if (this._balanceFactor(A.left) <= 0) {
@@ -17,7 +17,7 @@ export class BinaryIndexedTree {
17
17
  * @param - - `frequency`: The default frequency value. It is optional and has a default
18
18
  * value of 0.
19
19
  */
20
- constructor({frequency = 0, max}: {frequency?: number; max: number}) {
20
+ constructor({frequency = 0, max}: { frequency?: number; max: number }) {
21
21
  this._freq = frequency;
22
22
  this._max = max;
23
23
  this._freqMap = {0: 0};
@@ -6,7 +6,7 @@
6
6
  * @license MIT License
7
7
  */
8
8
 
9
- import type {OneParamCallback, BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions} from '../../types';
9
+ import type {BinaryTreeNodeKey, BinaryTreeNodeNested, BinaryTreeOptions, OneParamCallback} from '../../types';
10
10
  import {BinaryTreeDeletedResult, DFSOrderPattern, FamilyPosition, IterationType} from '../../types';
11
11
  import {IBinaryTree} from '../../interfaces';
12
12
  import {trampoline} from '../../utils';
@@ -108,8 +108,7 @@ export class BinaryTreeNode<V = any, N extends BinaryTreeNode<V, N> = BinaryTree
108
108
  * @template N - The type of the binary tree's nodes.
109
109
  */
110
110
  export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode<V, BinaryTreeNodeNested<V>>>
111
- implements IBinaryTree<V, N>
112
- {
111
+ implements IBinaryTree<V, N> {
113
112
  /**
114
113
  * Creates a new instance of BinaryTree.
115
114
  * @param {BinaryTreeOptions} [options] - The options for the binary tree.
@@ -395,7 +394,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
395
394
  return -1;
396
395
  }
397
396
 
398
- const stack: {node: N; depth: number}[] = [{node: beginRoot, depth: 0}];
397
+ const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
399
398
  let maxHeight = 0;
400
399
 
401
400
  while (stack.length > 0) {
@@ -832,7 +831,7 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
832
831
  _traverse(beginRoot);
833
832
  } else {
834
833
  // 0: visit, 1: print
835
- const stack: {opt: 0 | 1; node: N | null | undefined}[] = [{opt: 0, node: beginRoot}];
834
+ const stack: { opt: 0 | 1; node: N | null | undefined }[] = [{opt: 0, node: beginRoot}];
836
835
 
837
836
  while (stack.length > 0) {
838
837
  const cur = stack.pop();
@@ -1093,6 +1092,49 @@ export class BinaryTree<V = any, N extends BinaryTreeNode<V, N> = BinaryTreeNode
1093
1092
  return ans;
1094
1093
  }
1095
1094
 
1095
+ /**
1096
+ * The above function is an iterator for a binary tree that can be used to traverse the tree in
1097
+ * either an iterative or recursive manner.
1098
+ * @param node - The `node` parameter represents the current node in the binary tree from which the
1099
+ * iteration starts. It is an optional parameter with a default value of `this.root`, which means
1100
+ * that if no node is provided, the iteration will start from the root of the binary tree.
1101
+ * @returns The `*[Symbol.iterator]` method returns a generator object that yields the keys of the
1102
+ * binary tree nodes in a specific order.
1103
+ */
1104
+ * [Symbol.iterator](node = this.root): Generator<BinaryTreeNodeKey, void, undefined> {
1105
+ if (!node) {
1106
+ return;
1107
+ }
1108
+
1109
+ if (this.iterationType === IterationType.ITERATIVE) {
1110
+ const stack: (N | null | undefined)[] = [];
1111
+ let current: N | null | undefined = node;
1112
+
1113
+ while (current || stack.length > 0) {
1114
+ while (current) {
1115
+ stack.push(current);
1116
+ current = current.left;
1117
+ }
1118
+
1119
+ current = stack.pop();
1120
+
1121
+ if (current) yield current.key;
1122
+ if (current) current = current.right;
1123
+ }
1124
+ } else {
1125
+
1126
+ if (node.left) {
1127
+ yield* this[Symbol.iterator](node.left);
1128
+ }
1129
+ yield node.key;
1130
+ if (node.right) {
1131
+ yield* this[Symbol.iterator](node.right);
1132
+ }
1133
+ }
1134
+
1135
+ }
1136
+
1137
+
1096
1138
  /**
1097
1139
  * Swap the data of two nodes in the binary tree.
1098
1140
  * @param {N} srcNode - The source node to swap.
@@ -19,8 +19,7 @@ export class BSTNode<V = any, N extends BSTNode<V, N> = BSTNodeNested<V>> extend
19
19
 
20
20
  export class BST<V = any, N extends BSTNode<V, N> = BSTNode<V, BSTNodeNested<V>>>
21
21
  extends BinaryTree<V, N>
22
- implements IBinaryTree<V, N>
23
- {
22
+ implements IBinaryTree<V, N> {
24
23
  /**
25
24
  * The constructor function initializes a binary search tree object with an optional comparator
26
25
  * function.
@@ -21,8 +21,7 @@ export class RBTreeNode<V = any, N extends RBTreeNode<V, N> = RBTreeNodeNested<V
21
21
 
22
22
  export class RBTree<V, N extends RBTreeNode<V, N> = RBTreeNode<V, RBTreeNodeNested<V>>>
23
23
  extends BST<V, N>
24
- implements IBinaryTree<V, N>
25
- {
24
+ implements IBinaryTree<V, N> {
26
25
  constructor(options?: RBTreeOptions) {
27
26
  super(options);
28
27
  }
@@ -37,8 +37,7 @@ export class TreeMultisetNode<
37
37
  */
38
38
  export class TreeMultiset<V = any, N extends TreeMultisetNode<V, N> = TreeMultisetNode<V, TreeMultisetNodeNested<V>>>
39
39
  extends AVLTree<V, N>
40
- implements IBinaryTree<V, N>
41
- {
40
+ implements IBinaryTree<V, N> {
42
41
  /**
43
42
  * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
44
43
  * merge duplicated values.
@@ -105,8 +105,7 @@ export abstract class AbstractEdge<V = any> {
105
105
  export abstract class AbstractGraph<
106
106
  V extends AbstractVertex<any> = AbstractVertex<any>,
107
107
  E extends AbstractEdge<any> = AbstractEdge<any>
108
- > implements IGraph<V, E>
109
- {
108
+ > implements IGraph<V, E> {
110
109
  private _vertices: Map<VertexKey, V> = new Map<VertexKey, V>();
111
110
 
112
111
  get vertices(): Map<VertexKey, V> {
@@ -554,14 +553,14 @@ export abstract class AbstractGraph<
554
553
  }
555
554
 
556
555
  getMinDist &&
557
- distMap.forEach((d, v) => {
558
- if (v !== srcVertex) {
559
- if (d < minDist) {
560
- minDist = d;
561
- if (genPaths) minDest = v;
562
- }
556
+ distMap.forEach((d, v) => {
557
+ if (v !== srcVertex) {
558
+ if (d < minDist) {
559
+ minDist = d;
560
+ if (genPaths) minDest = v;
563
561
  }
564
- });
562
+ }
563
+ });
565
564
 
566
565
  genPaths && getPaths(minDest);
567
566
 
@@ -623,7 +622,7 @@ export abstract class AbstractGraph<
623
622
  if (vertexOrKey instanceof AbstractVertex) distMap.set(vertexOrKey, Infinity);
624
623
  }
625
624
 
626
- const heap = new PriorityQueue<{key: number; val: V}>({comparator: (a, b) => a.key - b.key});
625
+ const heap = new PriorityQueue<{ key: number; val: V }>({comparator: (a, b) => a.key - b.key});
627
626
  heap.add({key: 0, val: srcVertex});
628
627
 
629
628
  distMap.set(srcVertex, 0);
@@ -852,7 +851,7 @@ export abstract class AbstractGraph<
852
851
  * `predecessor` property is a 2D array of vertices (or `null`) representing the predecessor vertices in the shortest
853
852
  * path between vertices in the
854
853
  */
855
- floyd(): {costs: number[][]; predecessor: (V | null)[][]} {
854
+ floyd(): { costs: number[][]; predecessor: (V | null)[][] } {
856
855
  const idAndVertices = [...this._vertices];
857
856
  const n = idAndVertices.length;
858
857
 
@@ -64,8 +64,7 @@ export class DirectedEdge<V = any> extends AbstractEdge<V> {
64
64
 
65
65
  export class DirectedGraph<V extends DirectedVertex<any> = DirectedVertex, E extends DirectedEdge<any> = DirectedEdge>
66
66
  extends AbstractGraph<V, E>
67
- implements IGraph<V, E>
68
- {
67
+ implements IGraph<V, E> {
69
68
  /**
70
69
  * The constructor function initializes an instance of a class.
71
70
  */
@@ -51,12 +51,11 @@ export class UndirectedEdge<V = number> extends AbstractEdge<V> {
51
51
  }
52
52
 
53
53
  export class UndirectedGraph<
54
- V extends UndirectedVertex<any> = UndirectedVertex,
55
- E extends UndirectedEdge<any> = UndirectedEdge
56
- >
54
+ V extends UndirectedVertex<any> = UndirectedVertex,
55
+ E extends UndirectedEdge<any> = UndirectedEdge
56
+ >
57
57
  extends AbstractGraph<V, E>
58
- implements IGraph<V, E>
59
- {
58
+ implements IGraph<V, E> {
60
59
  /**
61
60
  * The constructor initializes a new Map object to store edges.
62
61
  */
@@ -157,7 +157,7 @@ export class HashMap<K, V> {
157
157
  }
158
158
  }
159
159
 
160
- *entries(): IterableIterator<[K, V]> {
160
+ * entries(): IterableIterator<[K, V]> {
161
161
  for (const bucket of this.table) {
162
162
  if (bucket) {
163
163
  for (const [key, value] of bucket) {
@@ -1 +1,2 @@
1
- export class TreeMap {}
1
+ export class TreeMap {
2
+ }
@@ -1 +1,2 @@
1
- export class TreeSet {}
1
+ export class TreeSet {
2
+ }
@@ -11,7 +11,7 @@ export class Heap<E = any> {
11
11
  protected nodes: E[] = [];
12
12
  protected readonly comparator: Comparator<E>;
13
13
 
14
- constructor(options: {comparator: Comparator<E>; nodes?: E[]}) {
14
+ constructor(options: { comparator: Comparator<E>; nodes?: E[] }) {
15
15
  this.comparator = options.comparator;
16
16
  if (options.nodes && options.nodes.length > 0) {
17
17
  this.nodes = options.nodes;
@@ -39,7 +39,7 @@ export class Heap<E = any> {
39
39
  * @returns A new Heap instance.
40
40
  * @param options
41
41
  */
42
- static heapify<E>(options: {nodes: E[]; comparator: Comparator<E>}): Heap<E> {
42
+ static heapify<E>(options: { nodes: E[]; comparator: Comparator<E> }): Heap<E> {
43
43
  return new Heap<E>(options);
44
44
  }
45
45
 
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MaxHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- options: {comparator: Comparator<E>; nodes?: E[]} = {
14
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
15
15
  comparator: (a: E, b: E) => {
16
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
17
17
  throw new Error('The a, b params of compare function must be number');
@@ -11,7 +11,7 @@ import type {Comparator} from '../../types';
11
11
 
12
12
  export class MinHeap<E = any> extends Heap<E> {
13
13
  constructor(
14
- options: {comparator: Comparator<E>; nodes?: E[]} = {
14
+ options: { comparator: Comparator<E>; nodes?: E[] } = {
15
15
  comparator: (a: E, b: E) => {
16
16
  if (!(typeof a === 'number' && typeof b === 'number')) {
17
17
  throw new Error('The a, b params of compare function must be number');
@@ -147,11 +147,11 @@ export class DoublyLinkedList<E = any> {
147
147
  }
148
148
 
149
149
  /**
150
- * The `pollLast()` function removes and returns the value of the last node in a doubly linked list.
150
+ * The `popLast()` function removes and returns the value of the last node in a doubly linked list.
151
151
  * @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
152
152
  * list is empty, it returns null.
153
153
  */
154
- pollLast(): E | undefined {
154
+ popLast(): E | undefined {
155
155
  return this.pop();
156
156
  }
157
157
 
@@ -175,11 +175,11 @@ export class DoublyLinkedList<E = any> {
175
175
  }
176
176
 
177
177
  /**
178
- * The `pollFirst()` function removes and returns the value of the first node in a doubly linked list.
178
+ * The `popFirst()` function removes and returns the value of the first node in a doubly linked list.
179
179
  * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
180
180
  * list.
181
181
  */
182
- pollFirst(): E | undefined {
182
+ popFirst(): E | undefined {
183
183
  return this.shift();
184
184
  }
185
185
 
@@ -211,18 +211,18 @@ export class DoublyLinkedList<E = any> {
211
211
  }
212
212
 
213
213
  /**
214
- * The `peekFirst` function returns the first node in a doubly linked list, or null if the list is empty.
215
- * @returns The method `peekFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
214
+ * The `getFirst` function returns the first node in a doubly linked list, or null if the list is empty.
215
+ * @returns The method `getFirst()` returns the first node of the doubly linked list, or `null` if the list is empty.
216
216
  */
217
- peekFirst(): E | undefined {
217
+ getFirst(): E | undefined {
218
218
  return this.head?.val;
219
219
  }
220
220
 
221
221
  /**
222
- * The `peekLast` function returns the last node in a doubly linked list, or null if the list is empty.
223
- * @returns The method `peekLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
222
+ * The `getLast` function returns the last node in a doubly linked list, or null if the list is empty.
223
+ * @returns The method `getLast()` returns the last node of the doubly linked list, or `null` if the list is empty.
224
224
  */
225
- peekLast(): E | undefined {
225
+ getLast(): E | undefined {
226
226
  return this.tail?.val;
227
227
  }
228
228
 
@@ -266,7 +266,7 @@ export class DoublyLinkedList<E = any> {
266
266
  * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<E>` if a node with the specified value `val`
267
267
  * is found in the linked list. If no such node is found, it returns `null`.
268
268
  */
269
- findNode(val: E | null): DoublyLinkedListNode<E> | null {
269
+ getNode(val: E | null): DoublyLinkedListNode<E> | null {
270
270
  let current = this.head;
271
271
 
272
272
  while (current) {
@@ -310,6 +310,43 @@ export class DoublyLinkedList<E = any> {
310
310
  return true;
311
311
  }
312
312
 
313
+ /**
314
+ * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
315
+ * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
316
+ * before which the new value will be inserted. It can be either the value of the existing node or the existing node
317
+ * itself.
318
+ * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
319
+ * list.
320
+ * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
321
+ * insertion fails.
322
+ */
323
+ insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
324
+ let existingNode;
325
+
326
+ if (existingValueOrNode instanceof DoublyLinkedListNode) {
327
+ existingNode = existingValueOrNode;
328
+ } else {
329
+ existingNode = this.getNode(existingValueOrNode);
330
+ }
331
+
332
+ if (existingNode) {
333
+ const newNode = new DoublyLinkedListNode(newValue);
334
+ newNode.prev = existingNode.prev;
335
+ if (existingNode.prev) {
336
+ existingNode.prev.next = newNode;
337
+ }
338
+ newNode.next = existingNode;
339
+ existingNode.prev = newNode;
340
+ if (existingNode === this.head) {
341
+ this.head = newNode;
342
+ }
343
+ this._length++;
344
+ return true;
345
+ }
346
+
347
+ return false;
348
+ }
349
+
313
350
  /**
314
351
  * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
315
352
  * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
@@ -331,9 +368,6 @@ export class DoublyLinkedList<E = any> {
331
368
  return removedNode!.val;
332
369
  }
333
370
 
334
- delete(valOrNode: E): boolean;
335
- delete(valOrNode: DoublyLinkedListNode<E>): boolean;
336
-
337
371
  /**
338
372
  * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
339
373
  * @param {E | DoublyLinkedListNode<E>} valOrNode - The `valOrNode` parameter can accept either a value of type `E` or
@@ -347,7 +381,7 @@ export class DoublyLinkedList<E = any> {
347
381
  if (valOrNode instanceof DoublyLinkedListNode) {
348
382
  node = valOrNode;
349
383
  } else {
350
- node = this.findNode(valOrNode);
384
+ node = this.getNode(valOrNode);
351
385
  }
352
386
 
353
387
  if (node) {
@@ -437,14 +471,14 @@ export class DoublyLinkedList<E = any> {
437
471
  }
438
472
 
439
473
  /**
440
- * The `findLast` function iterates through a linked list from the last node to the first node and returns the last
474
+ * The `findBackward` function iterates through a linked list from the last node to the first node and returns the last
441
475
  * value that satisfies the given callback function, or null if no value satisfies the callback.
442
476
  * @param callback - A function that takes a value of type E as its parameter and returns a boolean value. This
443
477
  * function is used to determine whether a given value satisfies a certain condition.
444
- * @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
478
+ * @returns The method `findBackward` returns the last value in the linked list that satisfies the condition specified by
445
479
  * the callback function. If no value satisfies the condition, it returns `null`.
446
480
  */
447
- findLast(callback: (val: E) => boolean): E | null {
481
+ findBackward(callback: (val: E) => boolean): E | null {
448
482
  let current = this.tail;
449
483
  while (current) {
450
484
  if (callback(current.val)) {
@@ -456,10 +490,10 @@ export class DoublyLinkedList<E = any> {
456
490
  }
457
491
 
458
492
  /**
459
- * The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
460
- * @returns The `toArrayReverse()` function returns an array of type `E[]`.
493
+ * The `toArrayBackward` function converts a doubly linked list into an array in reverse order.
494
+ * @returns The `toArrayBackward()` function returns an array of type `E[]`.
461
495
  */
462
- toArrayReverse(): E[] {
496
+ toArrayBackward(): E[] {
463
497
  const array: E[] = [];
464
498
  let current = this.tail;
465
499
  while (current) {
@@ -555,9 +589,6 @@ export class DoublyLinkedList<E = any> {
555
589
  return accumulator;
556
590
  }
557
591
 
558
- insertAfter(existingValueOrNode: E, newValue: E): boolean;
559
- insertAfter(existingValueOrNode: DoublyLinkedListNode<E>, newValue: E): boolean;
560
-
561
592
  /**
562
593
  * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
563
594
  * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
@@ -573,7 +604,7 @@ export class DoublyLinkedList<E = any> {
573
604
  if (existingValueOrNode instanceof DoublyLinkedListNode) {
574
605
  existingNode = existingValueOrNode;
575
606
  } else {
576
- existingNode = this.findNode(existingValueOrNode);
607
+ existingNode = this.getNode(existingValueOrNode);
577
608
  }
578
609
 
579
610
  if (existingNode) {
@@ -595,39 +626,14 @@ export class DoublyLinkedList<E = any> {
595
626
  }
596
627
 
597
628
  /**
598
- * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
599
- * @param {E | DoublyLinkedListNode<E>} existingValueOrNode - The existing value or node in the doubly linked list
600
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
601
- * itself.
602
- * @param {E} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
603
- * list.
604
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
605
- * insertion fails.
629
+ * The function returns an iterator that iterates over the values of a linked list.
606
630
  */
607
- insertBefore(existingValueOrNode: E | DoublyLinkedListNode<E>, newValue: E): boolean {
608
- let existingNode;
609
-
610
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
611
- existingNode = existingValueOrNode;
612
- } else {
613
- existingNode = this.findNode(existingValueOrNode);
614
- }
631
+ * [Symbol.iterator]() {
632
+ let current = this.head;
615
633
 
616
- if (existingNode) {
617
- const newNode = new DoublyLinkedListNode(newValue);
618
- newNode.prev = existingNode.prev;
619
- if (existingNode.prev) {
620
- existingNode.prev.next = newNode;
621
- }
622
- newNode.next = existingNode;
623
- existingNode.prev = newNode;
624
- if (existingNode === this.head) {
625
- this.head = newNode;
626
- }
627
- this._length++;
628
- return true;
634
+ while (current) {
635
+ yield current.val;
636
+ current = current.next;
629
637
  }
630
-
631
- return false;
632
638
  }
633
639
  }