data-structure-typed 1.17.4 → 1.18.5

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 (293) hide show
  1. package/README.md +193 -66
  2. package/backup/recursive-type/src/assets/complexities-diff.jpg +0 -0
  3. package/backup/recursive-type/src/assets/data-structure-complexities.jpg +0 -0
  4. package/backup/recursive-type/src/assets/logo.png +0 -0
  5. package/backup/recursive-type/src/assets/overview-diagram-of-data-structures.png +0 -0
  6. package/backup/recursive-type/src/data-structures/binary-tree/aa-tree.ts +3 -0
  7. package/backup/recursive-type/src/data-structures/binary-tree/avl-tree.ts +288 -0
  8. package/backup/recursive-type/src/data-structures/binary-tree/b-tree.ts +3 -0
  9. package/backup/recursive-type/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  10. package/backup/recursive-type/src/data-structures/binary-tree/binary-tree.ts +1502 -0
  11. package/backup/recursive-type/src/data-structures/binary-tree/bst.ts +503 -0
  12. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  13. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  14. package/backup/recursive-type/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  15. package/backup/recursive-type/src/data-structures/binary-tree/index.ts +11 -0
  16. package/backup/recursive-type/src/data-structures/binary-tree/rb-tree.ts +110 -0
  17. package/backup/recursive-type/src/data-structures/binary-tree/segment-tree.ts +243 -0
  18. package/backup/recursive-type/src/data-structures/binary-tree/splay-tree.ts +3 -0
  19. package/backup/recursive-type/src/data-structures/binary-tree/tree-multiset.ts +55 -0
  20. package/backup/recursive-type/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  21. package/backup/recursive-type/src/data-structures/diagrams/README.md +5 -0
  22. package/backup/recursive-type/src/data-structures/graph/abstract-graph.ts +985 -0
  23. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  24. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  25. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  26. package/backup/recursive-type/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  27. package/backup/recursive-type/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  28. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  29. package/backup/recursive-type/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  30. package/backup/recursive-type/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  31. package/backup/recursive-type/src/data-structures/graph/diagrams/mst.jpg +0 -0
  32. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  33. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  34. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  35. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  36. package/backup/recursive-type/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  37. package/backup/recursive-type/src/data-structures/graph/directed-graph.ts +478 -0
  38. package/backup/recursive-type/src/data-structures/graph/index.ts +3 -0
  39. package/backup/recursive-type/src/data-structures/graph/undirected-graph.ts +293 -0
  40. package/backup/recursive-type/src/data-structures/hash/coordinate-map.ts +67 -0
  41. package/backup/recursive-type/src/data-structures/hash/coordinate-set.ts +56 -0
  42. package/backup/recursive-type/src/data-structures/hash/hash-table.ts +3 -0
  43. package/backup/recursive-type/src/data-structures/hash/index.ts +6 -0
  44. package/backup/recursive-type/src/data-structures/hash/pair.ts +3 -0
  45. package/backup/recursive-type/src/data-structures/hash/tree-map.ts +3 -0
  46. package/backup/recursive-type/src/data-structures/hash/tree-set.ts +3 -0
  47. package/backup/recursive-type/src/data-structures/heap/heap.ts +176 -0
  48. package/backup/recursive-type/src/data-structures/heap/index.ts +3 -0
  49. package/backup/recursive-type/src/data-structures/heap/max-heap.ts +31 -0
  50. package/backup/recursive-type/src/data-structures/heap/min-heap.ts +34 -0
  51. package/backup/recursive-type/src/data-structures/index.ts +15 -0
  52. package/backup/recursive-type/src/data-structures/interfaces/abstract-graph.ts +42 -0
  53. package/backup/recursive-type/src/data-structures/interfaces/avl-tree.ts +1 -0
  54. package/backup/recursive-type/src/data-structures/interfaces/binary-tree.ts +56 -0
  55. package/backup/recursive-type/src/data-structures/interfaces/bst.ts +1 -0
  56. package/backup/recursive-type/src/data-structures/interfaces/directed-graph.ts +15 -0
  57. package/backup/recursive-type/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  58. package/backup/recursive-type/src/data-structures/interfaces/heap.ts +1 -0
  59. package/backup/recursive-type/src/data-structures/interfaces/index.ts +13 -0
  60. package/backup/recursive-type/src/data-structures/interfaces/navigator.ts +1 -0
  61. package/backup/recursive-type/src/data-structures/interfaces/priority-queue.ts +1 -0
  62. package/backup/recursive-type/src/data-structures/interfaces/segment-tree.ts +1 -0
  63. package/backup/recursive-type/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  64. package/backup/recursive-type/src/data-structures/interfaces/tree-multiset.ts +1 -0
  65. package/backup/recursive-type/src/data-structures/interfaces/undirected-graph.ts +3 -0
  66. package/backup/recursive-type/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  67. package/backup/recursive-type/src/data-structures/linked-list/index.ts +3 -0
  68. package/backup/recursive-type/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  69. package/backup/recursive-type/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  70. package/backup/recursive-type/src/data-structures/matrix/index.ts +4 -0
  71. package/backup/recursive-type/src/data-structures/matrix/matrix.ts +27 -0
  72. package/backup/recursive-type/src/data-structures/matrix/matrix2d.ts +208 -0
  73. package/backup/recursive-type/src/data-structures/matrix/navigator.ts +122 -0
  74. package/backup/recursive-type/src/data-structures/matrix/vector2d.ts +316 -0
  75. package/backup/recursive-type/src/data-structures/priority-queue/index.ts +3 -0
  76. package/backup/recursive-type/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  77. package/backup/recursive-type/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  78. package/backup/recursive-type/src/data-structures/priority-queue/priority-queue.ts +354 -0
  79. package/backup/recursive-type/src/data-structures/queue/deque.ts +251 -0
  80. package/backup/recursive-type/src/data-structures/queue/index.ts +2 -0
  81. package/backup/recursive-type/src/data-structures/queue/queue.ts +120 -0
  82. package/backup/recursive-type/src/data-structures/stack/index.ts +1 -0
  83. package/backup/recursive-type/src/data-structures/stack/stack.ts +98 -0
  84. package/backup/recursive-type/src/data-structures/tree/index.ts +1 -0
  85. package/backup/recursive-type/src/data-structures/tree/tree.ts +80 -0
  86. package/backup/recursive-type/src/data-structures/trie/index.ts +1 -0
  87. package/backup/recursive-type/src/data-structures/trie/trie.ts +227 -0
  88. package/backup/recursive-type/src/data-structures/types/abstract-graph.ts +5 -0
  89. package/backup/recursive-type/src/data-structures/types/avl-tree.ts +8 -0
  90. package/backup/recursive-type/src/data-structures/types/binary-tree.ts +10 -0
  91. package/backup/recursive-type/src/data-structures/types/bst.ts +6 -0
  92. package/backup/recursive-type/src/data-structures/types/directed-graph.ts +8 -0
  93. package/backup/recursive-type/src/data-structures/types/doubly-linked-list.ts +1 -0
  94. package/backup/recursive-type/src/data-structures/types/heap.ts +5 -0
  95. package/backup/recursive-type/src/data-structures/types/index.ts +12 -0
  96. package/backup/recursive-type/src/data-structures/types/navigator.ts +13 -0
  97. package/backup/recursive-type/src/data-structures/types/priority-queue.ts +9 -0
  98. package/backup/recursive-type/src/data-structures/types/segment-tree.ts +1 -0
  99. package/backup/recursive-type/src/data-structures/types/singly-linked-list.ts +1 -0
  100. package/backup/recursive-type/src/data-structures/types/tree-multiset.ts +1 -0
  101. package/backup/recursive-type/src/index.ts +1 -0
  102. package/backup/recursive-type/src/utils/index.ts +2 -0
  103. package/backup/recursive-type/src/utils/types/index.ts +1 -0
  104. package/backup/recursive-type/src/utils/types/utils.ts +6 -0
  105. package/backup/recursive-type/src/utils/utils.ts +78 -0
  106. package/dist/data-structures/binary-tree/avl-tree.d.ts +19 -25
  107. package/dist/data-structures/binary-tree/avl-tree.js +12 -20
  108. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +3 -1
  109. package/dist/data-structures/binary-tree/binary-indexed-tree.js +10 -0
  110. package/dist/data-structures/binary-tree/binary-tree.d.ts +135 -161
  111. package/dist/data-structures/binary-tree/binary-tree.js +192 -164
  112. package/dist/data-structures/binary-tree/bst.d.ts +21 -21
  113. package/dist/data-structures/binary-tree/bst.js +33 -36
  114. package/dist/data-structures/binary-tree/rb-tree.d.ts +1 -2
  115. package/dist/data-structures/binary-tree/rb-tree.js +68 -5
  116. package/dist/data-structures/binary-tree/segment-tree.d.ts +17 -39
  117. package/dist/data-structures/binary-tree/segment-tree.js +34 -47
  118. package/dist/data-structures/binary-tree/tree-multiset.d.ts +9 -8
  119. package/dist/data-structures/binary-tree/tree-multiset.js +7 -6
  120. package/dist/data-structures/graph/abstract-graph.d.ts +56 -71
  121. package/dist/data-structures/graph/abstract-graph.js +87 -92
  122. package/dist/data-structures/graph/directed-graph.d.ts +128 -105
  123. package/dist/data-structures/graph/directed-graph.js +161 -121
  124. package/dist/data-structures/graph/undirected-graph.d.ts +81 -62
  125. package/dist/data-structures/graph/undirected-graph.js +99 -78
  126. package/dist/data-structures/hash/coordinate-map.d.ts +1 -5
  127. package/dist/data-structures/hash/coordinate-map.js +3 -9
  128. package/dist/data-structures/hash/coordinate-set.d.ts +2 -6
  129. package/dist/data-structures/hash/coordinate-set.js +3 -9
  130. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  131. package/dist/data-structures/hash/hash-table.js +7 -0
  132. package/dist/data-structures/hash/pair.d.ts +2 -1
  133. package/dist/data-structures/hash/pair.js +7 -0
  134. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  135. package/dist/data-structures/hash/tree-map.js +7 -0
  136. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  137. package/dist/data-structures/hash/tree-set.js +7 -0
  138. package/dist/data-structures/heap/heap.d.ts +0 -14
  139. package/dist/data-structures/heap/heap.js +3 -27
  140. package/dist/data-structures/index.d.ts +1 -0
  141. package/dist/data-structures/index.js +1 -0
  142. package/dist/data-structures/interfaces/abstract-graph.d.ts +22 -0
  143. package/dist/data-structures/interfaces/abstract-graph.js +2 -0
  144. package/dist/data-structures/interfaces/avl-tree.d.ts +1 -0
  145. package/dist/data-structures/interfaces/avl-tree.js +2 -0
  146. package/dist/data-structures/interfaces/binary-tree.d.ts +27 -0
  147. package/dist/data-structures/interfaces/binary-tree.js +2 -0
  148. package/dist/data-structures/interfaces/bst.d.ts +1 -0
  149. package/dist/data-structures/interfaces/bst.js +2 -0
  150. package/dist/data-structures/interfaces/directed-graph.d.ts +9 -0
  151. package/dist/data-structures/interfaces/directed-graph.js +2 -0
  152. package/dist/data-structures/interfaces/doubly-linked-list.d.ts +1 -0
  153. package/dist/data-structures/interfaces/doubly-linked-list.js +2 -0
  154. package/dist/data-structures/interfaces/heap.d.ts +1 -0
  155. package/dist/data-structures/interfaces/heap.js +2 -0
  156. package/dist/data-structures/interfaces/index.d.ts +13 -0
  157. package/dist/data-structures/interfaces/index.js +29 -0
  158. package/dist/data-structures/interfaces/navigator.d.ts +1 -0
  159. package/dist/data-structures/interfaces/navigator.js +2 -0
  160. package/dist/data-structures/interfaces/priority-queue.d.ts +1 -0
  161. package/dist/data-structures/interfaces/priority-queue.js +2 -0
  162. package/dist/data-structures/interfaces/segment-tree.d.ts +1 -0
  163. package/dist/data-structures/interfaces/segment-tree.js +2 -0
  164. package/dist/data-structures/interfaces/singly-linked-list.d.ts +1 -0
  165. package/dist/data-structures/interfaces/singly-linked-list.js +2 -0
  166. package/dist/data-structures/interfaces/tree-multiset.d.ts +1 -0
  167. package/dist/data-structures/interfaces/tree-multiset.js +2 -0
  168. package/dist/data-structures/interfaces/undirected-graph.d.ts +2 -0
  169. package/dist/data-structures/interfaces/undirected-graph.js +2 -0
  170. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +1 -3
  171. package/dist/data-structures/linked-list/doubly-linked-list.js +12 -18
  172. package/dist/data-structures/linked-list/singly-linked-list.d.ts +1 -2
  173. package/dist/data-structures/linked-list/singly-linked-list.js +12 -15
  174. package/dist/data-structures/priority-queue/priority-queue.d.ts +1 -1
  175. package/dist/data-structures/priority-queue/priority-queue.js +4 -4
  176. package/dist/data-structures/queue/deque.d.ts +5 -5
  177. package/dist/data-structures/queue/deque.js +6 -6
  178. package/dist/data-structures/queue/queue.d.ts +1 -1
  179. package/dist/data-structures/stack/stack.d.ts +1 -1
  180. package/dist/data-structures/tree/tree.d.ts +12 -4
  181. package/dist/data-structures/tree/tree.js +44 -12
  182. package/dist/data-structures/trie/trie.d.ts +3 -3
  183. package/dist/data-structures/trie/trie.js +10 -10
  184. package/dist/data-structures/types/abstract-graph.d.ts +1 -20
  185. package/dist/data-structures/types/avl-tree.d.ts +5 -4
  186. package/dist/data-structures/types/binary-tree.d.ts +6 -5
  187. package/dist/data-structures/types/bst.d.ts +4 -3
  188. package/dist/data-structures/types/directed-graph.d.ts +5 -9
  189. package/dist/data-structures/types/directed-graph.js +7 -0
  190. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -1
  191. package/dist/data-structures/types/heap.d.ts +2 -2
  192. package/dist/data-structures/types/index.d.ts +0 -1
  193. package/dist/data-structures/types/index.js +0 -1
  194. package/dist/data-structures/types/navigator.d.ts +2 -2
  195. package/dist/data-structures/types/priority-queue.d.ts +2 -2
  196. package/dist/data-structures/types/tree-multiset.d.ts +3 -4
  197. package/docs/assets/search.js +1 -1
  198. package/docs/classes/AVLTree.html +552 -405
  199. package/docs/classes/AVLTreeNode.html +107 -242
  200. package/docs/classes/AaTree.html +18 -13
  201. package/docs/classes/AbstractEdge.html +77 -68
  202. package/docs/classes/AbstractGraph.html +223 -115
  203. package/docs/classes/AbstractVertex.html +71 -45
  204. package/docs/classes/ArrayDeque.html +31 -26
  205. package/docs/classes/BST.html +543 -391
  206. package/docs/classes/BSTNode.html +107 -236
  207. package/docs/classes/BTree.html +18 -13
  208. package/docs/classes/BinaryIndexedTree.html +56 -21
  209. package/docs/classes/BinaryTree.html +564 -355
  210. package/docs/classes/BinaryTreeNode.html +146 -199
  211. package/docs/classes/Character.html +21 -16
  212. package/docs/classes/CoordinateMap.html +49 -52
  213. package/docs/classes/CoordinateSet.html +50 -53
  214. package/docs/classes/Deque.html +51 -68
  215. package/docs/classes/DirectedEdge.html +98 -103
  216. package/docs/classes/DirectedGraph.html +449 -210
  217. package/docs/classes/DirectedVertex.html +63 -52
  218. package/docs/classes/DoublyLinkedList.html +56 -71
  219. package/docs/classes/DoublyLinkedListNode.html +28 -23
  220. package/docs/classes/HashTable.html +155 -0
  221. package/docs/classes/Heap.html +33 -109
  222. package/docs/classes/HeapItem.html +25 -20
  223. package/docs/classes/Matrix2D.html +33 -28
  224. package/docs/classes/MatrixNTI2D.html +21 -16
  225. package/docs/classes/MaxHeap.html +33 -114
  226. package/docs/classes/MaxPriorityQueue.html +71 -61
  227. package/docs/classes/MinHeap.html +33 -114
  228. package/docs/classes/MinPriorityQueue.html +71 -61
  229. package/docs/classes/Navigator.html +28 -23
  230. package/docs/classes/ObjectDeque.html +66 -51
  231. package/docs/classes/{RBTree.html → Pair.html} +25 -20
  232. package/docs/classes/PriorityQueue.html +66 -56
  233. package/docs/classes/Queue.html +33 -28
  234. package/docs/classes/SegmentTree.html +129 -57
  235. package/docs/classes/SegmentTreeNode.html +62 -140
  236. package/docs/classes/SinglyLinkedList.html +53 -58
  237. package/docs/classes/SinglyLinkedListNode.html +25 -20
  238. package/docs/classes/SkipLinkedList.html +18 -13
  239. package/docs/classes/SplayTree.html +18 -13
  240. package/docs/classes/Stack.html +31 -26
  241. package/docs/classes/TreeMap.html +155 -0
  242. package/docs/classes/TreeMultiSet.html +541 -388
  243. package/docs/classes/TreeNode.html +125 -35
  244. package/docs/classes/TreeSet.html +155 -0
  245. package/docs/classes/Trie.html +30 -25
  246. package/docs/classes/TrieNode.html +33 -28
  247. package/docs/classes/TwoThreeTree.html +18 -13
  248. package/docs/classes/UndirectedEdge.html +87 -80
  249. package/docs/classes/UndirectedGraph.html +356 -178
  250. package/docs/classes/UndirectedVertex.html +63 -52
  251. package/docs/classes/Vector2D.html +45 -40
  252. package/docs/enums/CP.html +21 -16
  253. package/docs/enums/FamilyPosition.html +21 -16
  254. package/docs/enums/LoopType.html +20 -15
  255. package/docs/{interfaces/AVLTreeDeleted.html → enums/TopologicalProperty.html} +47 -44
  256. package/docs/index.html +203 -69
  257. package/docs/interfaces/{PriorityQueueOptions.html → IBinaryTree.html} +49 -40
  258. package/docs/interfaces/IBinaryTreeNode.html +383 -0
  259. package/docs/interfaces/IDirectedGraph.html +24 -19
  260. package/docs/interfaces/IGraph.html +122 -89
  261. package/docs/interfaces/{HeapOptions.html → IUNDirectedGraph.html} +26 -53
  262. package/docs/modules.html +33 -23
  263. package/docs/types/{ToThunkFn.html → AVLTreeDeleted.html} +31 -22
  264. package/docs/types/BSTComparator.html +18 -13
  265. package/docs/types/BSTDeletedResult.html +23 -18
  266. package/docs/types/BinaryTreeDeleted.html +23 -18
  267. package/docs/types/BinaryTreeNodeId.html +18 -13
  268. package/docs/types/BinaryTreeNodePropertyName.html +18 -13
  269. package/docs/types/DFSOrderPattern.html +18 -13
  270. package/docs/types/DijkstraResult.html +18 -13
  271. package/docs/types/Direction.html +18 -13
  272. package/docs/types/{DoublyLinkedListGetBy.html → EdgeId.html} +22 -17
  273. package/docs/types/{TrlFn.html → HeapOptions.html} +33 -20
  274. package/docs/types/{TrlAsyncFn.html → NavigatorParams.html} +46 -20
  275. package/docs/types/NodeOrPropertyName.html +18 -13
  276. package/docs/types/PriorityQueueComparator.html +18 -13
  277. package/docs/types/PriorityQueueDFSOrderPattern.html +18 -13
  278. package/docs/types/{SpecifyOptional.html → PriorityQueueOptions.html} +32 -20
  279. package/docs/types/RecursiveAVLTreeNode.html +135 -0
  280. package/docs/types/RecursiveBSTNode.html +135 -0
  281. package/docs/types/RecursiveBinaryTreeNode.html +135 -0
  282. package/docs/types/ResultByProperty.html +21 -16
  283. package/docs/types/ResultsByProperty.html +21 -16
  284. package/docs/types/SegmentTreeNodeVal.html +18 -13
  285. package/docs/types/TopologicalStatus.html +18 -13
  286. package/docs/types/TreeMultiSetDeletedResult.html +23 -18
  287. package/docs/types/Turning.html +18 -13
  288. package/docs/types/VertexId.html +18 -13
  289. package/notes/note.md +12 -1
  290. package/package.json +11 -3
  291. package/tsconfig.json +2 -2
  292. package/docs/interfaces/NavigatorParams.html +0 -197
  293. package/docs/types/Thunk.html +0 -133
@@ -0,0 +1,293 @@
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
+ import {arrayRemove} from '../../utils';
9
+ import {AbstractEdge, AbstractGraph, AbstractVertex} from './abstract-graph';
10
+ import type {VertexId} from '../types';
11
+
12
+ export class UndirectedVertex<T = number> extends AbstractVertex<T> {
13
+ /**
14
+ * The constructor function initializes a vertex with an optional value.
15
+ * @param {VertexId} id - The `id` parameter is the identifier for the vertex. It is of type `VertexId`, which is
16
+ * typically a unique identifier for each vertex in a graph.
17
+ * @param {T} [val] - The "val" parameter is an optional parameter of type T. It is used to initialize the value of the
18
+ * vertex. If no value is provided, the vertex will be initialized with a default value.
19
+ */
20
+ constructor(id: VertexId, val?: T) {
21
+ super(id, val);
22
+ }
23
+
24
+ // _createVertex(id: VertexId, val?: T): T {
25
+ // return new T(id, val);
26
+ // }
27
+ }
28
+
29
+ export class UndirectedEdge<T = number> extends AbstractEdge<T> {
30
+ /**
31
+ * The constructor function initializes an instance of a class with two vertex IDs, an optional weight, and an optional
32
+ * value.
33
+ * @param {VertexId} v1 - The parameter `v1` is of type `VertexId` and represents the first vertex in the edge.
34
+ * @param {VertexId} v2 - The parameter `v2` is a `VertexId`, which represents the identifier of the second vertex in a
35
+ * graph edge.
36
+ * @param {number} [weight] - The weight parameter is an optional number that represents the weight of the edge.
37
+ * @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value associated with
38
+ * the edge.
39
+ */
40
+ constructor(v1: VertexId, v2: VertexId, weight?: number, val?: T) {
41
+ super(weight, val);
42
+ this._vertices = [v1, v2];
43
+ }
44
+
45
+ private _vertices: [VertexId, VertexId];
46
+
47
+ get vertices() {
48
+ return this._vertices;
49
+ }
50
+
51
+ set vertices(v: [VertexId, VertexId]) {
52
+ this._vertices = v;
53
+ }
54
+
55
+ // _createEdge(src: VertexId, dest: VertexId, weight?: number, val?: T): T {
56
+ // if (weight === undefined || weight === null) weight = 1;
57
+ // return new UndirectedEdge(src, dest, weight, val);
58
+ // }
59
+ }
60
+
61
+ export class UndirectedGraph<V extends UndirectedVertex<any>, E extends UndirectedEdge<any>> extends AbstractGraph<V, E> {
62
+
63
+ private readonly _vertexConstructor: new (id: VertexId, val?: V['val']) => V;
64
+ private readonly _edgeConstructor: new (src: VertexId, dest: VertexId, weight?: number, val?: E['val']) => E;
65
+
66
+ constructor(vertexConstructor: new (id: VertexId, val?: V['val']) => V, edgeConstructor: new (src: VertexId, dest: VertexId, weight?: number, val?: E['val']) => E) {
67
+ super();
68
+ this._vertexConstructor = vertexConstructor;
69
+ this._edgeConstructor = edgeConstructor;
70
+ this._edges = new Map<V, E[]>();
71
+ }
72
+
73
+ protected _edges: Map<V, E[]>;
74
+
75
+ get edges(): Map<V, E[]> {
76
+ return this._edges;
77
+ }
78
+
79
+ /**
80
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
81
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
82
+ * @param id
83
+ * @param val
84
+ */
85
+ _createVertex(id: VertexId, val?: V['val']): V {
86
+ return new this._vertexConstructor(id, val);
87
+ }
88
+
89
+ /**
90
+ * In TypeScript, a subclass inherits the interface implementation of its parent class, without needing to implement the same interface again in the subclass. This behavior differs from Java's approach. In Java, if a parent class implements an interface, the subclass needs to explicitly implement the same interface, even if the parent class has already implemented it.
91
+ * This means that using abstract methods in the parent class cannot constrain the grandchild classes. Defining methods within an interface also cannot constrain the descendant classes. When inheriting from this class, developers need to be aware that this method needs to be overridden.
92
+ * @param src
93
+ * @param dest
94
+ * @param weight
95
+ * @param val
96
+ */
97
+ _createEdge(src: VertexId, dest: VertexId, weight?: number, val?: E['val']): E {
98
+ if (weight === undefined || weight === null) weight = 1;
99
+ return new this._edgeConstructor(src, dest, weight, val);
100
+ }
101
+
102
+ /**
103
+ * The function `getEdge` returns the first undirected edge that connects two given vertices, or null if no such edge
104
+ * exists.
105
+ * @param {V | null | VertexId} v1 - The parameter `v1` represents either an `V`
106
+ * object, `null`, or a `VertexId`. It is used to specify one of the vertices of the edge.
107
+ * @param {V | null | VertexId} v2 - The parameter `v2` represents either an `UndirectedVertex`
108
+ * object or a `VertexId` (identifier) of an undirected vertex.
109
+ * @returns an instance of `E` or `null`.
110
+ */
111
+ getEdge(v1: V | null | VertexId, v2: V | null | VertexId): E | null {
112
+ let edges: E[] | undefined = [];
113
+
114
+ if (v1 !== null && v2 !== null) {
115
+ const vertex1: V | null = this._getVertex(v1);
116
+ const vertex2: V | null = this._getVertex(v2);
117
+
118
+ if (vertex1 && vertex2) {
119
+ edges = this._edges.get(vertex1)?.filter(e => e.vertices.includes(vertex2.id));
120
+ }
121
+ }
122
+
123
+ return edges ? edges[0] || null : null;
124
+ }
125
+
126
+ /**
127
+ * The function adds an undirected edge to a graph by updating the adjacency list.
128
+ * @param edge - An object representing an undirected edge in a graph. It has a property called "vertices" which is an
129
+ * array of two vertices connected by the edge.
130
+ * @returns a boolean value.
131
+ */
132
+ addEdge(edge: E): boolean {
133
+ for (const end of edge.vertices) {
134
+ const endVertex = this._getVertex(end);
135
+ if (endVertex === null) return false;
136
+ if (endVertex) {
137
+ const edges = this._edges.get(endVertex);
138
+ if (edges) {
139
+ edges.push(edge);
140
+ } else {
141
+ this._edges.set(endVertex, [edge]);
142
+ }
143
+ }
144
+ }
145
+ return true;
146
+ }
147
+
148
+ /**
149
+ * The function removes an edge between two vertices in an undirected graph.
150
+ * @param {V | VertexId} v1 - The parameter `v1` represents either an `V` object or
151
+ * a `VertexId`. It is used to specify one of the vertices of the edge that needs to be removed.
152
+ * @param {V | VertexId} v2 - The parameter `v2` represents either an instance of the
153
+ * `UndirectedVertex` class or a `VertexId`. It is used to identify the second vertex of the edge that needs to be
154
+ * removed.
155
+ * @returns The function `removeEdgeBetween` returns an `E` object if an edge is successfully removed
156
+ * between the two vertices `v1` and `v2`. If either `v1` or `v2` is not found in the graph, or if there is no edge
157
+ * between them, the function returns `null`.
158
+ */
159
+ removeEdgeBetween(v1: V | VertexId, v2: V | VertexId): E | null {
160
+
161
+ const vertex1: V | null = this._getVertex(v1);
162
+ const vertex2: V | null = this._getVertex(v2);
163
+
164
+ if (!vertex1 || !vertex2) {
165
+ return null;
166
+ }
167
+
168
+ const v1Edges = this._edges.get(vertex1);
169
+ let removed: E | null = null;
170
+ if (v1Edges) {
171
+ removed = arrayRemove<E>(v1Edges, (e: E) => e.vertices.includes(vertex2.id))[0] || null;
172
+ }
173
+ const v2Edges = this._edges.get(vertex2);
174
+ if (v2Edges) {
175
+ arrayRemove<E>(v2Edges, (e: E) => e.vertices.includes(vertex1.id));
176
+ }
177
+ return removed;
178
+ }
179
+
180
+ /**
181
+ * The removeEdge function removes an edge between two vertices in an undirected graph.
182
+ * @param edge - An object representing an undirected edge. It has a property called "vertices" which is an array
183
+ * containing the two vertices connected by the edge.
184
+ * @returns The method is returning an UndirectedEdge object or null.
185
+ */
186
+ removeEdge(edge: E): E | null {
187
+ return this.removeEdgeBetween(edge.vertices[0], edge.vertices[1]);
188
+ }
189
+
190
+ /**
191
+ * The function "degreeOf" returns the degree of a given vertex in an undirected graph.
192
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or an
193
+ * `V`.
194
+ * @returns the degree of the vertex.
195
+ */
196
+ degreeOf(vertexOrId: VertexId | V): number {
197
+ const vertex = this._getVertex(vertexOrId);
198
+ if (vertex) {
199
+ return this._edges.get(vertex)?.length || 0;
200
+ } else {
201
+ return 0;
202
+ }
203
+ }
204
+
205
+ /**
206
+ * The function "edgesOf" returns an array of undirected edges connected to a given vertex or vertex ID.
207
+ * @param {VertexId | V} vertexOrId - The parameter `vertexOrId` can be either a `VertexId` or an
208
+ * `V`.
209
+ * @returns an array of UndirectedEdge objects.
210
+ */
211
+ edgesOf(vertexOrId: VertexId | V): E[] {
212
+ const vertex = this._getVertex(vertexOrId);
213
+ if (vertex) {
214
+ return this._edges.get(vertex) || [];
215
+ } else {
216
+ return [];
217
+ }
218
+ }
219
+
220
+ /**
221
+ * The function "edgeSet" returns an array of unique undirected edges from a set of edges.
222
+ * @returns The method `edgeSet()` returns an array of `E` objects.
223
+ */
224
+ edgeSet(): E[] {
225
+ const edgeSet: Set<E> = new Set();
226
+ this._edges.forEach(edges => {
227
+ edges.forEach(edge => {
228
+ edgeSet.add(edge);
229
+ });
230
+ });
231
+ return [...edgeSet];
232
+ }
233
+
234
+ /**
235
+ * The function "getEdgesOf" returns an array of undirected edges connected to a given vertex or vertex ID.
236
+ * @param {V | VertexId} vertexOrId - The parameter `vertexOrId` can be either an
237
+ * `V` object or a `VertexId`.
238
+ * @returns The function `getEdgesOf` returns an array of `E` objects.
239
+ */
240
+ getEdgesOf(vertexOrId: V | VertexId): E[] {
241
+ const vertex = this._getVertex(vertexOrId);
242
+ if (!vertex) {
243
+ return [];
244
+ }
245
+ return this._edges.get(vertex) || [];
246
+ }
247
+
248
+ /**
249
+ * The function `getNeighbors` returns an array of neighboring vertices of a given vertex in an undirected graph.
250
+ * @param {V | VertexId} vertexOrId - The `vertexOrId` parameter can be either an
251
+ * `V` object or a `VertexId`. It represents the vertex for which we want to find the neighbors.
252
+ * @returns an array of UndirectedVertex objects.
253
+ */
254
+ getNeighbors(vertexOrId: V | VertexId): V[] {
255
+ const neighbors: V[] = [];
256
+ const vertex = this._getVertex(vertexOrId);
257
+ if (vertex) {
258
+ const neighborEdges = this.getEdgesOf(vertex);
259
+ for (const edge of neighborEdges) {
260
+ const neighbor = this._getVertex(edge.vertices.filter(e => e !== vertex.id)[0]);
261
+ if (neighbor) {
262
+ neighbors.push(neighbor);
263
+ }
264
+ }
265
+ }
266
+ return neighbors;
267
+ }
268
+
269
+ /**
270
+ * The function "getEndsOfEdge" returns the two vertices that form the ends of a given undirected edge, or null if the
271
+ * edge does not exist in the graph.
272
+ * @param edge - An object representing an undirected edge in a graph. It has a property called "vertices" which is an
273
+ * array containing two vertices that the edge connects.
274
+ * @returns The function `getEndsOfEdge` returns an array containing the two ends of the given `edge` if the edge
275
+ * exists in the graph. If the edge does not exist, it returns `null`.
276
+ */
277
+ getEndsOfEdge(edge: E): [V, V] | null {
278
+ if (!this.hasEdge(edge.vertices[0], edge.vertices[1])) {
279
+ return null;
280
+ }
281
+ const v1 = this._getVertex(edge.vertices[0]);
282
+ const v2 = this._getVertex(edge.vertices[1]);
283
+ if (v1 && v2) {
284
+ return [v1, v2];
285
+ } else {
286
+ return null;
287
+ }
288
+ }
289
+
290
+ protected _setEdges(v: Map<V, E[]>) {
291
+ this._edges = v;
292
+ }
293
+ }
@@ -0,0 +1,67 @@
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 CoordinateMap<V> extends Map<any, V> {
9
+ constructor(joint?: string) {
10
+ super();
11
+ if (joint !== undefined) this._joint = joint;
12
+ }
13
+
14
+ protected _joint: string = '_';
15
+
16
+ get joint(): string {
17
+ return this._joint;
18
+ }
19
+
20
+ /**
21
+ * The "has" function overrides the base class's "has" function and checks if a key exists in the map by joining the
22
+ * key array with a specified delimiter.
23
+ * @param {number[]} key - The parameter "key" is an array of numbers.
24
+ * @returns The `has` method is being overridden to return the result of calling the `has` method of the superclass
25
+ * (`super.has`) with the `key` array joined together using the `_joint` property.
26
+ */
27
+ override has(key: number[]) {
28
+ return super.has(key.join(this._joint));
29
+ }
30
+
31
+ /**
32
+ * The function overrides the set method of a Map object to convert the key from an array to a string using a specified
33
+ * delimiter before calling the original set method.
34
+ * @param {number[]} key - The key parameter is an array of numbers.
35
+ * @param {V} value - The value parameter is the value that you want to associate with the specified key.
36
+ * @returns The `set` method is returning the result of calling the `set` method of the superclass
37
+ * (`super.set(key.join(this._joint), value)`).
38
+ */
39
+ override set(key: number[], value: V) {
40
+ return super.set(key.join(this._joint), value);
41
+ }
42
+
43
+ /**
44
+ * The function overrides the get method to join the key array with a specified joint and then calls the super get
45
+ * method.
46
+ * @param {number[]} key - An array of numbers
47
+ * @returns The code is returning the value associated with the specified key in the map.
48
+ */
49
+ override get(key: number[]) {
50
+ return super.get(key.join(this._joint));
51
+ }
52
+
53
+ /**
54
+ * The function overrides the delete method and joins the key array using a specified joint character before calling
55
+ * the super delete method.
56
+ * @param {number[]} key - An array of numbers that represents the key to be deleted.
57
+ * @returns The `delete` method is returning the result of calling the `delete` method on the superclass, with the
58
+ * `key` array joined together using the `_joint` property.
59
+ */
60
+ override delete(key: number[]) {
61
+ return super.delete(key.join(this._joint));
62
+ }
63
+
64
+ protected _setJoint(v: string) {
65
+ this._joint = v;
66
+ }
67
+ }
@@ -0,0 +1,56 @@
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 CoordinateSet extends Set<any> {
9
+ constructor(joint?: string) {
10
+ super();
11
+ if (joint !== undefined) this._joint = joint;
12
+ }
13
+
14
+ protected _joint: string = '_';
15
+
16
+ get joint(): string {
17
+ return this._joint;
18
+ }
19
+
20
+ /**
21
+ * The "has" function overrides the "has" method of the superclass and checks if a value exists in an array after
22
+ * joining its elements with a specified separator.
23
+ * @param {number[]} value - The parameter "value" is an array of numbers.
24
+ * @returns The overridden `has` method is returning the result of calling the `has` method of the superclass, passing
25
+ * in the joined value as an argument.
26
+ */
27
+ override has(value: number[]) {
28
+ return super.has(value.join(this._joint));
29
+ }
30
+
31
+ /**
32
+ * The "add" function overrides the parent class's "add" function by joining the elements of the input array with a
33
+ * specified delimiter before calling the parent class's "add" function.
34
+ * @param {number[]} value - An array of numbers
35
+ * @returns The overridden `add` method is returning the result of calling the `add` method of the superclass
36
+ * (`super.add`) with the joined string representation of the `value` array (`value.join(this._joint)`).
37
+ */
38
+ override add(value: number[]) {
39
+ return super.add(value.join(this._joint));
40
+ }
41
+
42
+ /**
43
+ * The function overrides the delete method and deletes an element from a Set by joining the elements of the input
44
+ * array with a specified joint and then calling the delete method of the parent class.
45
+ * @param {number[]} value - An array of numbers
46
+ * @returns The `delete` method is returning the result of calling the `delete` method of the superclass, with the
47
+ * `value` array joined together using the `_joint` property.
48
+ */
49
+ override delete(value: number[]) {
50
+ return super.delete(value.join(this._joint));
51
+ }
52
+
53
+ protected _setJoint(v: string) {
54
+ this._joint = v;
55
+ }
56
+ }
@@ -0,0 +1,3 @@
1
+ export class HashTable {
2
+
3
+ }
@@ -0,0 +1,6 @@
1
+ export * from './hash-table';
2
+ export * from './coordinate-map';
3
+ export * from './coordinate-set';
4
+ export * from './pair';
5
+ export * from './tree-map';
6
+ export * from './tree-set';
@@ -0,0 +1,3 @@
1
+ export class Pair {
2
+
3
+ }
@@ -0,0 +1,3 @@
1
+ export class TreeMap {
2
+
3
+ }
@@ -0,0 +1,3 @@
1
+ export class TreeSet {
2
+
3
+ }
@@ -0,0 +1,176 @@
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
+ import {PriorityQueue} from '../priority-queue';
9
+ import type {HeapOptions} from '../types';
10
+
11
+ export class HeapItem<T = number> {
12
+
13
+ constructor(priority: number = NaN, val: T | null = null) {
14
+ this._val = val;
15
+ this._priority = priority;
16
+ }
17
+
18
+ private _priority: number;
19
+
20
+ get priority(): number {
21
+ return this._priority;
22
+ }
23
+
24
+ set priority(value: number) {
25
+ this._priority = value;
26
+ }
27
+
28
+ private _val: T | null;
29
+
30
+ get val(): T | null {
31
+ return this._val;
32
+ }
33
+
34
+ set val(value: T | null) {
35
+ this._val = value;
36
+ }
37
+ }
38
+
39
+ export abstract class Heap<T = number> {
40
+ /**
41
+ * The function is a constructor for a class that initializes a priority callback function based on the
42
+ * options provided.
43
+ * @param [options] - An optional object that contains configuration options for the Heap.
44
+ */
45
+ protected constructor(options?: HeapOptions<T>) {
46
+ if (options) {
47
+ const {priority} = options;
48
+ if (priority !== undefined && typeof priority !== 'function') {
49
+ throw new Error('.constructor expects a valid priority function');
50
+ }
51
+ this._priorityCb = priority || ((el) => +el);
52
+ } else {
53
+ this._priorityCb = (el) => +el;
54
+ }
55
+ }
56
+
57
+ protected abstract _pq: PriorityQueue<HeapItem<T>>;
58
+
59
+ get pq() {
60
+ return this._pq;
61
+ }
62
+
63
+ protected _priorityCb: (val: T) => number;
64
+ get priorityCb() {
65
+ return this._priorityCb;
66
+ }
67
+
68
+ /**
69
+ * The function returns the size of a priority queue.
70
+ * @returns The size of the priority queue.
71
+ */
72
+ get size(): number {
73
+ return this._pq.size;
74
+ }
75
+
76
+ /**
77
+ * The function checks if a priority queue is empty.
78
+ * @returns {boolean} A boolean value indicating whether the size of the priority queue is less than 1.
79
+ */
80
+ isEmpty(): boolean {
81
+ return this._pq.size < 1;
82
+ }
83
+
84
+ /**
85
+ * The `peek` function returns the top item in the priority queue without removing it.
86
+ * @returns The `peek()` method is returning either a `HeapItem<T>` object or `null`.Returns an val with the highest priority in the queue
87
+ */
88
+ peek(): HeapItem<T> | null {
89
+ return this._pq.peek();
90
+ }
91
+
92
+ /**
93
+ * The `peekLast` function returns the last item in the heap.
94
+ * @returns The method `peekLast()` returns either a `HeapItem<T>` object or `null`.Returns an val with the lowest priority in the queue
95
+ */
96
+ peekLast(): HeapItem<T> | null {
97
+ return this._pq.leaf();
98
+ }
99
+
100
+ /**
101
+ * The `add` function adds an val to a priority queue with an optional priority value.
102
+ * @param {T} val - The `val` parameter represents the value that you want to add to the heap. It can be of any
103
+ * type.
104
+ * @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
105
+ * val being added to the heap. If the `val` parameter is a number, then the `priority` parameter is set to
106
+ * the value of `val`. If the `val` parameter is not a number, then the
107
+ * @returns The `add` method returns the instance of the `Heap` class.
108
+ * @throws {Error} if priority is not a valid number
109
+ */
110
+ add(val: T, priority?: number): Heap<T> {
111
+ if (typeof val === 'number') {
112
+ priority = val;
113
+ } else {
114
+ if (priority === undefined) {
115
+ throw new Error('.add expects a numeric priority');
116
+ }
117
+ }
118
+
119
+ if (priority && Number.isNaN(+priority)) {
120
+ throw new Error('.add expects a numeric priority');
121
+ }
122
+
123
+ if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(val))) {
124
+ throw new Error(
125
+ '.add expects a numeric priority '
126
+ + 'or a constructor callback that returns a number'
127
+ );
128
+ }
129
+
130
+ const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(val);
131
+ this._pq.add(new HeapItem<T>(_priority, val));
132
+ return this;
133
+ }
134
+
135
+ /**
136
+ * The `poll` function returns the top item from a priority queue or null if the queue is empty.Removes and returns an val with the highest priority in the queue
137
+ * @returns either a HeapItem<T> object or null.
138
+ */
139
+ poll(): HeapItem<T> | null {
140
+ const top = this._pq.poll();
141
+ if (!top) {
142
+ return null;
143
+ }
144
+ return top;
145
+ }
146
+
147
+ /**
148
+ * The function checks if a given node or value exists in the priority queue.
149
+ * @param {T | HeapItem<T>} node - The parameter `node` can be of type `T` or `HeapItem<T>`.
150
+ * @returns a boolean value.
151
+ */
152
+ has(node: T | HeapItem<T>): boolean {
153
+ if (node instanceof HeapItem) {
154
+ return this.pq.getNodes().includes(node);
155
+ } else {
156
+ return this.pq.getNodes().findIndex(item => {
157
+ return item.val === node;
158
+ }) !== -1;
159
+ }
160
+ }
161
+
162
+ /**
163
+ * The `toArray` function returns an array of `HeapItem<T>` objects.
164
+ * @returns An array of HeapItem<T> objects.Returns a sorted list of vals
165
+ */
166
+ toArray(): HeapItem<T>[] {
167
+ return this._pq.toArray();
168
+ }
169
+
170
+ /**
171
+ * The clear function clears the priority queue.
172
+ */
173
+ clear(): void {
174
+ this._pq.clear();
175
+ }
176
+ }
@@ -0,0 +1,3 @@
1
+ export * from './max-heap';
2
+ export * from './min-heap';
3
+ export * from './heap';
@@ -0,0 +1,31 @@
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
+
9
+ import {Heap, HeapItem} from './heap';
10
+ import {PriorityQueue} from '../priority-queue';
11
+ import type {HeapOptions} from '../types';
12
+
13
+ /**
14
+ * @class MaxHeap
15
+ * @extends Heap
16
+ */
17
+ export class MaxHeap<T = number> extends Heap<T> {
18
+ protected _pq: PriorityQueue<HeapItem<T>>;
19
+
20
+ /**
21
+ * The constructor initializes a PriorityQueue with a custom comparator function.
22
+ * @param [options] - The `options` parameter is an optional object that can be passed to the constructor. It is of
23
+ * type `HeapOptions<T>`, which is a generic type that represents the options for the heap.
24
+ */
25
+ constructor(options?: HeapOptions<T>) {
26
+ super(options);
27
+ this._pq = new PriorityQueue<HeapItem<T>>({
28
+ comparator: (a, b) => b.priority - a.priority
29
+ });
30
+ }
31
+ }