data-structure-typed 1.18.7 → 1.19.0

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 (269) hide show
  1. package/README.md +208 -419
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +198 -167
  3. package/dist/data-structures/binary-tree/abstract-binary-tree.js +413 -398
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +24 -16
  5. package/dist/data-structures/binary-tree/avl-tree.js +23 -17
  6. package/dist/data-structures/binary-tree/binary-tree.d.ts +12 -26
  7. package/dist/data-structures/binary-tree/binary-tree.js +12 -27
  8. package/dist/data-structures/binary-tree/bst.d.ts +66 -90
  9. package/dist/data-structures/binary-tree/bst.js +85 -163
  10. package/dist/data-structures/binary-tree/rb-tree.d.ts +6 -17
  11. package/dist/data-structures/binary-tree/rb-tree.js +5 -17
  12. package/dist/data-structures/binary-tree/tree-multiset.d.ts +187 -18
  13. package/dist/data-structures/binary-tree/tree-multiset.js +716 -39
  14. package/dist/data-structures/graph/abstract-graph.d.ts +112 -52
  15. package/dist/data-structures/graph/abstract-graph.js +138 -72
  16. package/dist/data-structures/graph/directed-graph.d.ts +104 -101
  17. package/dist/data-structures/graph/directed-graph.js +134 -129
  18. package/dist/data-structures/graph/undirected-graph.d.ts +66 -65
  19. package/dist/data-structures/graph/undirected-graph.js +107 -106
  20. package/dist/data-structures/interfaces/abstract-binary-tree.d.ts +12 -18
  21. package/dist/data-structures/interfaces/abstract-graph.d.ts +2 -4
  22. package/dist/data-structures/interfaces/avl-tree.d.ts +2 -2
  23. package/dist/data-structures/interfaces/binary-tree.d.ts +1 -1
  24. package/dist/data-structures/interfaces/bst.d.ts +4 -5
  25. package/dist/data-structures/interfaces/rb-tree.d.ts +2 -3
  26. package/dist/data-structures/interfaces/tree-multiset.d.ts +7 -1
  27. package/dist/data-structures/tree/tree.d.ts +1 -4
  28. package/dist/data-structures/tree/tree.js +1 -12
  29. package/dist/data-structures/types/abstract-binary-tree.d.ts +10 -6
  30. package/dist/data-structures/types/abstract-binary-tree.js +9 -5
  31. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  32. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  33. package/dist/data-structures/types/bst.d.ts +1 -1
  34. package/dist/data-structures/types/bst.js +1 -1
  35. package/dist/data-structures/types/directed-graph.js +1 -1
  36. package/dist/data-structures/types/helpers.d.ts +1 -8
  37. package/dist/data-structures/types/rb-tree.d.ts +2 -0
  38. package/dist/data-structures/types/rb-tree.js +1 -1
  39. package/dist/data-structures/types/tree-multiset.d.ts +5 -5
  40. package/dist/utils/index.d.ts +1 -0
  41. package/dist/utils/index.js +1 -0
  42. package/dist/utils/types/index.d.ts +1 -0
  43. package/dist/utils/types/index.js +1 -0
  44. package/dist/utils/types/validate-type.d.ts +19 -0
  45. package/dist/utils/types/validate-type.js +2 -0
  46. package/dist/utils/utils.d.ts +2 -2
  47. package/dist/utils/utils.js +0 -62
  48. package/dist/utils/validate-type.d.ts +45 -0
  49. package/dist/utils/validate-type.js +58 -0
  50. package/package.json +5 -2
  51. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  52. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  53. package/backup/recursive-type/src/assets/logo.png +0 -0
  54. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  55. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +0 -3
  56. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +0 -288
  57. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +0 -3
  58. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -78
  59. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +0 -1502
  60. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +0 -503
  61. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  62. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  63. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  64. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +0 -11
  65. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +0 -110
  66. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +0 -243
  67. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +0 -3
  68. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +0 -55
  69. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  70. package/backup/recursive-type/src/data-structures/diagrams/README.md +0 -5
  71. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +0 -985
  72. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  73. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  74. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  75. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  76. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  77. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  78. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  79. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  80. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  81. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  82. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  83. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  84. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  85. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  86. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +0 -478
  87. package/backup/recursive-type/src/data-structures/graph/index.ts +0 -3
  88. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +0 -293
  89. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +0 -67
  90. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +0 -56
  91. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +0 -3
  92. package/backup/recursive-type/src/data-structures/hash/index.ts +0 -6
  93. package/backup/recursive-type/src/data-structures/hash/pair.ts +0 -3
  94. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +0 -3
  95. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +0 -3
  96. package/backup/recursive-type/src/data-structures/heap/heap.ts +0 -176
  97. package/backup/recursive-type/src/data-structures/heap/index.ts +0 -3
  98. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +0 -31
  99. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +0 -34
  100. package/backup/recursive-type/src/data-structures/index.ts +0 -15
  101. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +0 -42
  102. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +0 -1
  103. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +0 -56
  104. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +0 -1
  105. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +0 -15
  106. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +0 -1
  107. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +0 -1
  108. package/backup/recursive-type/src/data-structures/interfaces/index.ts +0 -13
  109. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +0 -1
  110. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +0 -1
  111. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +0 -1
  112. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +0 -1
  113. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +0 -1
  114. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +0 -3
  115. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +0 -573
  116. package/backup/recursive-type/src/data-structures/linked-list/index.ts +0 -3
  117. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +0 -490
  118. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +0 -3
  119. package/backup/recursive-type/src/data-structures/matrix/index.ts +0 -4
  120. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +0 -27
  121. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +0 -208
  122. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +0 -122
  123. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +0 -316
  124. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +0 -3
  125. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +0 -49
  126. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +0 -50
  127. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +0 -354
  128. package/backup/recursive-type/src/data-structures/queue/deque.ts +0 -251
  129. package/backup/recursive-type/src/data-structures/queue/index.ts +0 -2
  130. package/backup/recursive-type/src/data-structures/queue/queue.ts +0 -120
  131. package/backup/recursive-type/src/data-structures/stack/index.ts +0 -1
  132. package/backup/recursive-type/src/data-structures/stack/stack.ts +0 -98
  133. package/backup/recursive-type/src/data-structures/tree/index.ts +0 -1
  134. package/backup/recursive-type/src/data-structures/tree/tree.ts +0 -80
  135. package/backup/recursive-type/src/data-structures/trie/index.ts +0 -1
  136. package/backup/recursive-type/src/data-structures/trie/trie.ts +0 -227
  137. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +0 -5
  138. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +0 -8
  139. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +0 -10
  140. package/backup/recursive-type/src/data-structures/types/bst.ts +0 -6
  141. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +0 -8
  142. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +0 -1
  143. package/backup/recursive-type/src/data-structures/types/heap.ts +0 -5
  144. package/backup/recursive-type/src/data-structures/types/index.ts +0 -12
  145. package/backup/recursive-type/src/data-structures/types/navigator.ts +0 -13
  146. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +0 -9
  147. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +0 -1
  148. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +0 -1
  149. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +0 -1
  150. package/backup/recursive-type/src/index.ts +0 -1
  151. package/backup/recursive-type/src/utils/index.ts +0 -2
  152. package/backup/recursive-type/src/utils/types/index.ts +0 -1
  153. package/backup/recursive-type/src/utils/types/utils.ts +0 -6
  154. package/backup/recursive-type/src/utils/utils.ts +0 -78
  155. package/docs/.nojekyll +0 -1
  156. package/docs/assets/highlight.css +0 -92
  157. package/docs/assets/main.js +0 -58
  158. package/docs/assets/search.js +0 -1
  159. package/docs/assets/style.css +0 -1367
  160. package/docs/classes/AVLTree.html +0 -2451
  161. package/docs/classes/AVLTreeNode.html +0 -499
  162. package/docs/classes/AaTree.html +0 -172
  163. package/docs/classes/AbstractBinaryTree.html +0 -2118
  164. package/docs/classes/AbstractBinaryTreeNode.html +0 -524
  165. package/docs/classes/AbstractEdge.html +0 -295
  166. package/docs/classes/AbstractGraph.html +0 -1043
  167. package/docs/classes/AbstractVertex.html +0 -258
  168. package/docs/classes/ArrayDeque.html +0 -439
  169. package/docs/classes/BST.html +0 -2297
  170. package/docs/classes/BSTNode.html +0 -503
  171. package/docs/classes/BTree.html +0 -172
  172. package/docs/classes/BinaryIndexedTree.html +0 -341
  173. package/docs/classes/BinaryTree.html +0 -2133
  174. package/docs/classes/BinaryTreeNode.html +0 -501
  175. package/docs/classes/Character.html +0 -220
  176. package/docs/classes/CoordinateMap.html +0 -483
  177. package/docs/classes/CoordinateSet.html +0 -444
  178. package/docs/classes/Deque.html +0 -975
  179. package/docs/classes/DirectedEdge.html +0 -366
  180. package/docs/classes/DirectedGraph.html +0 -1443
  181. package/docs/classes/DirectedVertex.html +0 -254
  182. package/docs/classes/DoublyLinkedList.html +0 -968
  183. package/docs/classes/DoublyLinkedListNode.html +0 -297
  184. package/docs/classes/HashTable.html +0 -172
  185. package/docs/classes/Heap.html +0 -423
  186. package/docs/classes/HeapItem.html +0 -255
  187. package/docs/classes/Matrix2D.html +0 -502
  188. package/docs/classes/MatrixNTI2D.html +0 -240
  189. package/docs/classes/MaxHeap.html +0 -436
  190. package/docs/classes/MaxPriorityQueue.html +0 -836
  191. package/docs/classes/MinHeap.html +0 -437
  192. package/docs/classes/MinPriorityQueue.html +0 -838
  193. package/docs/classes/Navigator.html +0 -313
  194. package/docs/classes/ObjectDeque.html +0 -455
  195. package/docs/classes/Pair.html +0 -172
  196. package/docs/classes/PriorityQueue.html +0 -760
  197. package/docs/classes/Queue.html +0 -392
  198. package/docs/classes/RBTree.html +0 -2388
  199. package/docs/classes/RBTreeNode.html +0 -516
  200. package/docs/classes/SegmentTree.html +0 -434
  201. package/docs/classes/SegmentTreeNode.html +0 -357
  202. package/docs/classes/SinglyLinkedList.html +0 -788
  203. package/docs/classes/SinglyLinkedListNode.html +0 -270
  204. package/docs/classes/SkipLinkedList.html +0 -172
  205. package/docs/classes/SplayTree.html +0 -172
  206. package/docs/classes/Stack.html +0 -368
  207. package/docs/classes/TreeMap.html +0 -172
  208. package/docs/classes/TreeMultiSet.html +0 -2297
  209. package/docs/classes/TreeMultiSetNode.html +0 -499
  210. package/docs/classes/TreeNode.html +0 -343
  211. package/docs/classes/TreeSet.html +0 -172
  212. package/docs/classes/Trie.html +0 -372
  213. package/docs/classes/TrieNode.html +0 -280
  214. package/docs/classes/TwoThreeTree.html +0 -172
  215. package/docs/classes/UndirectedEdge.html +0 -337
  216. package/docs/classes/UndirectedGraph.html +0 -1210
  217. package/docs/classes/UndirectedVertex.html +0 -254
  218. package/docs/classes/Vector2D.html +0 -805
  219. package/docs/enums/CP.html +0 -181
  220. package/docs/enums/FamilyPosition.html +0 -181
  221. package/docs/enums/LoopType.html +0 -182
  222. package/docs/enums/RBColor.html +0 -174
  223. package/docs/enums/TopologicalProperty.html +0 -181
  224. package/docs/index.html +0 -645
  225. package/docs/interfaces/IAVLTree.html +0 -1378
  226. package/docs/interfaces/IAVLTreeNode.html +0 -405
  227. package/docs/interfaces/IAbstractBinaryTree.html +0 -1124
  228. package/docs/interfaces/IAbstractBinaryTreeNode.html +0 -384
  229. package/docs/interfaces/IAbstractGraph.html +0 -433
  230. package/docs/interfaces/IBST.html +0 -1271
  231. package/docs/interfaces/IBSTNode.html +0 -408
  232. package/docs/interfaces/IDirectedGraph.html +0 -572
  233. package/docs/interfaces/IUNDirectedGraph.html +0 -465
  234. package/docs/modules.html +0 -262
  235. package/docs/types/AVLTreeOptions.html +0 -150
  236. package/docs/types/AbstractBinaryTreeNodeProperties.html +0 -152
  237. package/docs/types/AbstractBinaryTreeNodeProperty.html +0 -152
  238. package/docs/types/AbstractBinaryTreeOptions.html +0 -156
  239. package/docs/types/AbstractRecursiveBinaryTreeNode.html +0 -152
  240. package/docs/types/BSTComparator.html +0 -162
  241. package/docs/types/BSTOptions.html +0 -152
  242. package/docs/types/BinaryTreeDeletedResult.html +0 -159
  243. package/docs/types/BinaryTreeNodeId.html +0 -147
  244. package/docs/types/BinaryTreeNodePropertyName.html +0 -147
  245. package/docs/types/BinaryTreeOptions.html +0 -150
  246. package/docs/types/DFSOrderPattern.html +0 -147
  247. package/docs/types/DijkstraResult.html +0 -167
  248. package/docs/types/Direction.html +0 -147
  249. package/docs/types/EdgeId.html +0 -147
  250. package/docs/types/HeapOptions.html +0 -168
  251. package/docs/types/IdObject.html +0 -157
  252. package/docs/types/KeyValObject.html +0 -152
  253. package/docs/types/NavigatorParams.html +0 -181
  254. package/docs/types/NodeOrPropertyName.html +0 -147
  255. package/docs/types/PriorityQueueComparator.html +0 -167
  256. package/docs/types/PriorityQueueDFSOrderPattern.html +0 -147
  257. package/docs/types/PriorityQueueOptions.html +0 -161
  258. package/docs/types/RBTreeOptions.html +0 -150
  259. package/docs/types/RecursiveAVLTreeNode.html +0 -152
  260. package/docs/types/RecursiveBSTNode.html +0 -152
  261. package/docs/types/RecursiveBinaryTreeNode.html +0 -152
  262. package/docs/types/RecursiveTreeMultiSetNode.html +0 -152
  263. package/docs/types/SegmentTreeNodeVal.html +0 -147
  264. package/docs/types/TopologicalStatus.html +0 -147
  265. package/docs/types/TreeMultiSetOptions.html +0 -152
  266. package/docs/types/Turning.html +0 -147
  267. package/docs/types/VertexId.html +0 -147
  268. package/notes/bst.test.ts +0 -181
  269. package/notes/note.md +0 -34
@@ -1,573 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- export class DoublyLinkedListNode<T = number> {
9
-
10
- /**
11
- * The constructor function initializes the value, next, and previous properties of an object.
12
- * @param {T} val - The "val" parameter is the value that will be stored in the node. It can be of any data type, as it
13
- * is defined as a generic type "T".
14
- */
15
- constructor(val: T) {
16
- this._val = val;
17
- this._next = null;
18
- this._prev = null;
19
- }
20
-
21
- private _val: T;
22
-
23
- get val(): T {
24
- return this._val;
25
- }
26
-
27
- set val(value: T) {
28
- this._val = value;
29
- }
30
-
31
- private _next: DoublyLinkedListNode<T> | null;
32
-
33
- get next(): DoublyLinkedListNode<T> | null {
34
- return this._next;
35
- }
36
-
37
- set next(value: DoublyLinkedListNode<T> | null) {
38
- this._next = value;
39
- }
40
-
41
- private _prev: DoublyLinkedListNode<T> | null;
42
-
43
- get prev(): DoublyLinkedListNode<T> | null {
44
- return this._prev;
45
- }
46
-
47
- set prev(value: DoublyLinkedListNode<T> | null) {
48
- this._prev = value;
49
- }
50
- }
51
-
52
- export class DoublyLinkedList<T = number> {
53
-
54
- /**
55
- * The constructor initializes the linked list with an empty head, tail, and length.
56
- */
57
- constructor() {
58
- this._head = null;
59
- this._tail = null;
60
- this._length = 0;
61
- }
62
-
63
- private _head: DoublyLinkedListNode<T> | null;
64
-
65
- get head(): DoublyLinkedListNode<T> | null {
66
- return this._head;
67
- }
68
-
69
- set head(value: DoublyLinkedListNode<T> | null) {
70
- this._head = value;
71
- }
72
-
73
- private _tail: DoublyLinkedListNode<T> | null;
74
-
75
- get tail(): DoublyLinkedListNode<T> | null {
76
- return this._tail;
77
- }
78
-
79
- set tail(value: DoublyLinkedListNode<T> | null) {
80
- this._tail = value;
81
- }
82
-
83
- private _length: number;
84
-
85
- get length(): number {
86
- return this._length;
87
- }
88
-
89
- /**
90
- * The `fromArray` function creates a new instance of a DoublyLinkedList and populates it with the elements from the
91
- * given array.
92
- * @param {T[]} data - The `data` parameter is an array of elements of type `T`.
93
- * @returns The `fromArray` function returns a DoublyLinkedList object.
94
- */
95
- static fromArray<T>(data: T[]) {
96
- const doublyLinkedList = new DoublyLinkedList<T>();
97
- for (const item of data) {
98
- doublyLinkedList.push(item);
99
- }
100
- return doublyLinkedList;
101
- }
102
-
103
- /**
104
- * The push function adds a new node with the given value to the end of the doubly linked list.
105
- * @param {T} val - The value to be added to the linked list.
106
- */
107
- push(val: T): void {
108
- const newNode = new DoublyLinkedListNode(val);
109
- if (!this.head) {
110
- this.head = newNode;
111
- this.tail = newNode;
112
- } else {
113
- newNode.prev = this.tail;
114
- this.tail!.next = newNode;
115
- this.tail = newNode;
116
- }
117
- this._length++;
118
- }
119
-
120
- /**
121
- * The `pop()` function removes and returns the value of the last node in a doubly linked list.
122
- * @returns The method is returning the value of the removed node (removedNode.val) if the list is not empty. If the
123
- * list is empty, it returns null.
124
- */
125
- pop(): T | null {
126
- if (!this.tail) return null;
127
- const removedNode = this.tail;
128
- if (this.head === this.tail) {
129
- this.head = null;
130
- this.tail = null;
131
- } else {
132
- this.tail = removedNode.prev;
133
- this.tail!.next = null;
134
- }
135
- this._length--;
136
- return removedNode.val;
137
- }
138
-
139
- /**
140
- * The `shift()` function removes and returns the value of the first node in a doubly linked list.
141
- * @returns The method `shift()` returns the value of the node that is removed from the beginning of the doubly linked
142
- * list.
143
- */
144
- shift(): T | null {
145
- if (!this.head) return null;
146
- const removedNode = this.head;
147
- if (this.head === this.tail) {
148
- this.head = null;
149
- this.tail = null;
150
- } else {
151
- this.head = removedNode.next;
152
- this.head!.prev = null;
153
- }
154
- this._length--;
155
- return removedNode.val;
156
- }
157
-
158
- /**
159
- * The unshift function adds a new node with the given value to the beginning of a doubly linked list.
160
- * @param {T} val - The `val` parameter represents the value of the new node that will be added to the beginning of the
161
- * doubly linked list.
162
- */
163
- unshift(val: T): void {
164
- const newNode = new DoublyLinkedListNode(val);
165
- if (!this.head) {
166
- this.head = newNode;
167
- this.tail = newNode;
168
- } else {
169
- newNode.next = this.head;
170
- this.head!.prev = newNode;
171
- this.head = newNode;
172
- }
173
- this._length++;
174
- }
175
-
176
- /**
177
- * The `getAt` function returns the value at a specified index in a linked list, or null if the index is out of bounds.
178
- * @param {number} index - The index parameter is a number that represents the position of the element we want to
179
- * retrieve from the list.
180
- * @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds
181
- * or the linked list is empty, it will return null.
182
- */
183
- getAt(index: number): T | null {
184
- if (index < 0 || index >= this.length) return null;
185
- let current = this.head;
186
- for (let i = 0; i < index; i++) {
187
- current = current!.next;
188
- }
189
- return current!.val;
190
- }
191
-
192
- /**
193
- * The function `getNodeAt` returns the node at a given index in a doubly linked list, or null if the index is out of
194
- * range.
195
- * @param {number} index - The `index` parameter is a number that represents the position of the node we want to
196
- * retrieve from the doubly linked list. It indicates the zero-based index of the node we want to access.
197
- * @returns The method `getNodeAt(index: number)` returns a `DoublyLinkedListNode<T>` object if the index is within the
198
- * valid range of the linked list, otherwise it returns `null`.
199
- */
200
- getNodeAt(index: number): DoublyLinkedListNode<T> | null {
201
- if (index < 0 || index >= this.length) return null;
202
- let current = this.head;
203
- for (let i = 0; i < index; i++) {
204
- current = current!.next;
205
- }
206
- return current;
207
- }
208
-
209
- /**
210
- * The function `findNodeByValue` searches for a node with a specific value in a doubly linked list and returns the
211
- * node if found, otherwise it returns null.
212
- * @param {T} val - The `val` parameter is the value that we want to search for in the doubly linked list.
213
- * @returns The function `findNodeByValue` returns a `DoublyLinkedListNode<T>` if a node with the specified value `val`
214
- * is found in the linked list. If no such node is found, it returns `null`.
215
- */
216
- findNode(val: T): DoublyLinkedListNode<T> | null {
217
- let current = this.head;
218
-
219
- while (current) {
220
- if (current.val === val) {
221
- return current;
222
- }
223
- current = current.next;
224
- }
225
-
226
- return null;
227
- }
228
-
229
- /**
230
- * The `insert` function inserts a value at a specified index in a doubly linked list.
231
- * @param {number} index - The index parameter represents the position at which the new value should be inserted in the
232
- * DoublyLinkedList. It is of type number.
233
- * @param {T} val - The `val` parameter represents the value that you want to insert into the Doubly Linked List at the
234
- * specified index.
235
- * @returns The `insert` method returns a boolean value. It returns `true` if the insertion is successful, and `false`
236
- * if the index is out of bounds.
237
- */
238
- insertAt(index: number, val: T): boolean {
239
- if (index < 0 || index > this.length) return false;
240
- if (index === 0) {
241
- this.unshift(val);
242
- return true;
243
- }
244
- if (index === this.length) {
245
- this.push(val);
246
- return true;
247
- }
248
-
249
- const newNode = new DoublyLinkedListNode(val);
250
- const prevNode = this.getNodeAt(index - 1);
251
- const nextNode = prevNode!.next;
252
- newNode.prev = prevNode;
253
- newNode.next = nextNode;
254
- prevNode!.next = newNode;
255
- nextNode!.prev = newNode;
256
- this._length++;
257
- return true;
258
- }
259
-
260
- /**
261
- * The `deleteAt` function removes an element at a specified index from a linked list and returns the removed element.
262
- * @param {number} index - The index parameter represents the position of the element that needs to be deleted in the
263
- * data structure. It is of type number.
264
- * @returns The method `deleteAt` returns the value of the node that was deleted, or `null` if the index is out of
265
- * bounds.
266
- */
267
- deleteAt(index: number): T | null {
268
- if (index < 0 || index >= this.length) return null;
269
- if (index === 0) return this.shift();
270
- if (index === this.length - 1) return this.pop();
271
-
272
- const removedNode = this.getNodeAt(index);
273
- const prevNode = removedNode!.prev;
274
- const nextNode = removedNode!.next;
275
- prevNode!.next = nextNode;
276
- nextNode!.prev = prevNode;
277
- this._length--;
278
- return removedNode!.val;
279
- }
280
-
281
- delete(valOrNode: T): boolean;
282
- delete(valOrNode: DoublyLinkedListNode<T>): boolean;
283
- /**
284
- * The `delete` function removes a node from a doubly linked list based on either the node itself or its value.
285
- * @param {T | DoublyLinkedListNode<T>} valOrNode - The `valOrNode` parameter can accept either a value of type `T` or
286
- * a `DoublyLinkedListNode<T>` object.
287
- * @returns The `delete` method returns a boolean value. It returns `true` if the value or node was successfully
288
- * deleted from the doubly linked list, and `false` if the value or node was not found in the list.
289
- */
290
- delete(valOrNode: T | DoublyLinkedListNode<T>): boolean {
291
- let node: DoublyLinkedListNode<T> | null;
292
-
293
- if (valOrNode instanceof DoublyLinkedListNode) {
294
- node = valOrNode;
295
- } else {
296
- node = this.findNode(valOrNode);
297
- }
298
-
299
- if (node) {
300
- if (node === this.head) {
301
- this.shift();
302
- } else if (node === this.tail) {
303
- this.pop();
304
- } else {
305
- const prevNode = node.prev;
306
- const nextNode = node.next;
307
- prevNode!.next = nextNode;
308
- nextNode!.prev = prevNode;
309
- this._length--;
310
- }
311
- return true;
312
- }
313
- return false;
314
- }
315
-
316
- /**
317
- * The `toArray` function converts a linked list into an array.
318
- * @returns The `toArray()` method is returning an array of type `T[]`.
319
- */
320
- toArray(): T[] {
321
- const array: T[] = [];
322
- let current = this.head;
323
- while (current) {
324
- array.push(current.val);
325
- current = current.next;
326
- }
327
- return array;
328
- }
329
-
330
- /**
331
- * The `clear` function resets the linked list by setting the head, tail, and length to null and 0 respectively.
332
- */
333
- clear(): void {
334
- this._head = null;
335
- this._tail = null;
336
- this._length = 0;
337
- }
338
-
339
- /**
340
- * The `find` function iterates through a linked list and returns the first element that satisfies a given condition.
341
- * @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
342
- * function is used to determine whether a particular value in the linked list satisfies a certain condition.
343
- * @returns The method `find` returns the first element in the linked list that satisfies the condition specified by
344
- * the callback function. If no element satisfies the condition, it returns `null`.
345
- */
346
- find(callback: (val: T) => boolean): T | null {
347
- let current = this.head;
348
- while (current) {
349
- if (callback(current.val)) {
350
- return current.val;
351
- }
352
- current = current.next;
353
- }
354
- return null;
355
- }
356
-
357
- /**
358
- * The function returns the index of the first occurrence of a given value in a linked list.
359
- * @param {T} val - The parameter `val` is of type `T`, which means it can be any data type. It represents the value
360
- * that we are searching for in the linked list.
361
- * @returns The method `indexOf` returns the index of the first occurrence of the specified value `val` in the linked
362
- * list. If the value is not found, it returns -1.
363
- */
364
- indexOf(val: T): number {
365
- let index = 0;
366
- let current = this.head;
367
- while (current) {
368
- if (current.val === val) {
369
- return index;
370
- }
371
- index++;
372
- current = current.next;
373
- }
374
- return -1;
375
- }
376
-
377
- /**
378
- * The `findLast` function iterates through a linked list from the last node to the first node and returns the last
379
- * value that satisfies the given callback function, or null if no value satisfies the callback.
380
- * @param callback - A function that takes a value of type T as its parameter and returns a boolean value. This
381
- * function is used to determine whether a given value satisfies a certain condition.
382
- * @returns The method `findLast` returns the last value in the linked list that satisfies the condition specified by
383
- * the callback function. If no value satisfies the condition, it returns `null`.
384
- */
385
- findLast(callback: (val: T) => boolean): T | null {
386
- let current = this.tail;
387
- while (current) {
388
- if (callback(current.val)) {
389
- return current.val;
390
- }
391
- current = current.prev;
392
- }
393
- return null;
394
- }
395
-
396
- /**
397
- * The `toArrayReverse` function converts a doubly linked list into an array in reverse order.
398
- * @returns The `toArrayReverse()` function returns an array of type `T[]`.
399
- */
400
- toArrayReverse(): T[] {
401
- const array: T[] = [];
402
- let current = this.tail;
403
- while (current) {
404
- array.push(current.val);
405
- current = current.prev;
406
- }
407
- return array;
408
- }
409
-
410
- /**
411
- * The `reverse` function reverses the order of the elements in a doubly linked list.
412
- */
413
- reverse(): void {
414
- let current = this.head;
415
- [this.head, this.tail] = [this.tail, this.head];
416
- while (current) {
417
- const next = current.next;
418
- [current.prev, current.next] = [current.next, current.prev];
419
- current = next;
420
- }
421
- }
422
-
423
- /**
424
- * The `forEach` function iterates over each element in a linked list and applies a callback function to each element.
425
- * @param callback - The callback parameter is a function that takes two arguments: val and index. The val argument
426
- * represents the value of the current node in the linked list, and the index argument represents the index of the
427
- * current node in the linked list.
428
- */
429
- forEach(callback: (val: T, index: number) => void): void {
430
- let current = this.head;
431
- let index = 0;
432
- while (current) {
433
- callback(current.val, index);
434
- current = current.next;
435
- index++;
436
- }
437
- }
438
-
439
- /**
440
- * The `map` function takes a callback function and applies it to each element in the DoublyLinkedList, returning a new
441
- * DoublyLinkedList with the transformed values.
442
- * @param callback - The callback parameter is a function that takes a value of type T (the type of values stored in
443
- * the original DoublyLinkedList) and returns a value of type U (the type of values that will be stored in the mapped
444
- * DoublyLinkedList).
445
- * @returns The `map` function is returning a new instance of `DoublyLinkedList<U>` that contains the mapped values.
446
- */
447
- map<U>(callback: (val: T) => U): DoublyLinkedList<U> {
448
- const mappedList = new DoublyLinkedList<U>();
449
- let current = this.head;
450
- while (current) {
451
- mappedList.push(callback(current.val));
452
- current = current.next;
453
- }
454
- return mappedList;
455
- }
456
-
457
- /**
458
- * The `filter` function iterates through a DoublyLinkedList and returns a new DoublyLinkedList containing only the
459
- * elements that satisfy the given callback function.
460
- * @param callback - The `callback` parameter is a function that takes a value of type `T` and returns a boolean value.
461
- * It is used to determine whether a value should be included in the filtered list or not.
462
- * @returns The filtered list, which is an instance of the DoublyLinkedList class.
463
- */
464
- filter(callback: (val: T) => boolean): DoublyLinkedList<T> {
465
- const filteredList = new DoublyLinkedList<T>();
466
- let current = this.head;
467
- while (current) {
468
- if (callback(current.val)) {
469
- filteredList.push(current.val);
470
- }
471
- current = current.next;
472
- }
473
- return filteredList;
474
- }
475
-
476
- /**
477
- * The `reduce` function iterates over a linked list and applies a callback function to each element, accumulating a
478
- * single value.
479
- * @param callback - The `callback` parameter is a function that takes two arguments: `accumulator` and `val`. It is
480
- * used to perform a specific operation on each element of the linked list.
481
- * @param {U} initialValue - The `initialValue` parameter is the initial value of the accumulator. It is the starting
482
- * point for the reduction operation.
483
- * @returns The `reduce` method is returning the final value of the accumulator after iterating through all the
484
- * elements in the linked list.
485
- */
486
- reduce<U>(callback: (accumulator: U, val: T) => U, initialValue: U): U {
487
- let accumulator = initialValue;
488
- let current = this.head;
489
- while (current) {
490
- accumulator = callback(accumulator, current.val);
491
- current = current.next;
492
- }
493
- return accumulator;
494
- }
495
-
496
- insertAfter(existingValueOrNode: T, newValue: T): boolean;
497
- insertAfter(existingValueOrNode: DoublyLinkedListNode<T>, newValue: T): boolean;
498
- /**
499
- * The `insertAfter` function inserts a new node with a given value after an existing node in a doubly linked list.
500
- * @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
501
- * after which the new value will be inserted. It can be either the value of the existing node or the existing node
502
- * itself.
503
- * @param {T} newValue - The value that you want to insert into the doubly linked list.
504
- * @returns The method returns a boolean value. It returns true if the insertion is successful, and false if the
505
- * existing value or node is not found in the doubly linked list.
506
- */
507
- insertAfter(existingValueOrNode: T | DoublyLinkedListNode<T>, newValue: T): boolean {
508
- let existingNode;
509
-
510
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
511
- existingNode = existingValueOrNode;
512
- } else {
513
- existingNode = this.findNode(existingValueOrNode);
514
- }
515
-
516
- if (existingNode) {
517
- const newNode = new DoublyLinkedListNode(newValue);
518
- newNode.next = existingNode.next;
519
- if (existingNode.next) {
520
- existingNode.next.prev = newNode;
521
- }
522
- newNode.prev = existingNode;
523
- existingNode.next = newNode;
524
- if (existingNode === this.tail) {
525
- this.tail = newNode;
526
- }
527
- this._length++;
528
- return true;
529
- }
530
-
531
- return false;
532
- }
533
-
534
- insertBefore(existingValueOrNode: T, newValue: T): boolean;
535
- insertBefore(existingValueOrNode: DoublyLinkedListNode<T>, newValue: T): boolean;
536
- /**
537
- * The `insertBefore` function inserts a new value before an existing value or node in a doubly linked list.
538
- * @param {T | DoublyLinkedListNode<T>} existingValueOrNode - The existing value or node in the doubly linked list
539
- * before which the new value will be inserted. It can be either the value of the existing node or the existing node
540
- * itself.
541
- * @param {T} newValue - The `newValue` parameter represents the value that you want to insert into the doubly linked
542
- * list.
543
- * @returns The method returns a boolean value. It returns `true` if the insertion is successful, and `false` if the
544
- * insertion fails.
545
- */
546
- insertBefore(existingValueOrNode: T | DoublyLinkedListNode<T>, newValue: T): boolean {
547
- let existingNode;
548
-
549
- if (existingValueOrNode instanceof DoublyLinkedListNode) {
550
- existingNode = existingValueOrNode;
551
- } else {
552
- existingNode = this.findNode(existingValueOrNode);
553
- }
554
-
555
- if (existingNode) {
556
- const newNode = new DoublyLinkedListNode(newValue);
557
- newNode.prev = existingNode.prev;
558
- if (existingNode.prev) {
559
- existingNode.prev.next = newNode;
560
- }
561
- newNode.next = existingNode;
562
- existingNode.prev = newNode;
563
- if (existingNode === this.head) {
564
- this.head = newNode;
565
- }
566
- this._length++;
567
- return true;
568
- }
569
-
570
- return false;
571
- }
572
- }
573
-
@@ -1,3 +0,0 @@
1
- export * from './singly-linked-list';
2
- export * from './doubly-linked-list';
3
- export * from './skip-linked-list';