data-structure-typed 1.18.7 → 1.18.8

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