data-structure-typed 0.9.16 → 1.12.10

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 (199) hide show
  1. package/.dependency-cruiser.js +449 -0
  2. package/.idea/data-structure-typed.iml +2 -0
  3. package/README.md +102 -1
  4. package/dist/data-structures/binary-tree/avl-tree.d.ts +57 -0
  5. package/dist/data-structures/binary-tree/avl-tree.js +57 -0
  6. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +27 -0
  7. package/dist/data-structures/binary-tree/binary-indexed-tree.js +27 -0
  8. package/dist/data-structures/binary-tree/binary-tree.d.ts +215 -9
  9. package/dist/data-structures/binary-tree/binary-tree.js +283 -15
  10. package/dist/data-structures/binary-tree/bst.d.ts +16 -0
  11. package/dist/data-structures/binary-tree/bst.js +12 -0
  12. package/dist/data-structures/binary-tree/segment-tree.d.ts +4 -0
  13. package/dist/data-structures/binary-tree/segment-tree.js +4 -0
  14. package/dist/data-structures/binary-tree/tree-multiset.d.ts +4 -0
  15. package/dist/data-structures/binary-tree/tree-multiset.js +4 -0
  16. package/dist/data-structures/graph/abstract-graph.d.ts +163 -13
  17. package/dist/data-structures/graph/abstract-graph.js +166 -12
  18. package/dist/data-structures/graph/directed-graph.d.ts +111 -1
  19. package/dist/data-structures/graph/directed-graph.js +122 -0
  20. package/dist/data-structures/graph/undirected-graph.d.ts +63 -0
  21. package/dist/data-structures/graph/undirected-graph.js +67 -0
  22. package/dist/data-structures/hash/coordinate-map.d.ts +32 -0
  23. package/dist/data-structures/hash/coordinate-map.js +32 -0
  24. package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
  25. package/dist/data-structures/hash/coordinate-set.js +25 -0
  26. package/dist/data-structures/heap/heap.d.ts +26 -31
  27. package/dist/data-structures/heap/heap.js +23 -32
  28. package/dist/data-structures/heap/max-heap.d.ts +6 -1
  29. package/dist/data-structures/heap/max-heap.js +6 -1
  30. package/dist/data-structures/heap/min-heap.d.ts +7 -1
  31. package/dist/data-structures/heap/min-heap.js +7 -1
  32. package/dist/data-structures/index.d.ts +0 -1
  33. package/dist/data-structures/index.js +0 -1
  34. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -6
  35. package/dist/data-structures/linked-list/doubly-linked-list.js +45 -8
  36. package/dist/data-structures/linked-list/singly-linked-list.d.ts +11 -8
  37. package/dist/data-structures/linked-list/singly-linked-list.js +4 -0
  38. package/dist/data-structures/matrix/matrix.d.ts +9 -0
  39. package/dist/data-structures/matrix/matrix.js +11 -0
  40. package/dist/data-structures/matrix/matrix2d.d.ts +82 -2
  41. package/dist/data-structures/matrix/matrix2d.js +82 -2
  42. package/dist/data-structures/matrix/navigator.d.ts +32 -0
  43. package/dist/data-structures/matrix/navigator.js +28 -0
  44. package/dist/data-structures/matrix/vector2d.d.ts +139 -15
  45. package/dist/data-structures/matrix/vector2d.js +139 -15
  46. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +8 -0
  47. package/dist/data-structures/priority-queue/max-priority-queue.js +8 -0
  48. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +8 -0
  49. package/dist/data-structures/priority-queue/min-priority-queue.js +8 -0
  50. package/dist/data-structures/priority-queue/priority-queue.d.ts +130 -0
  51. package/dist/data-structures/priority-queue/priority-queue.js +126 -0
  52. package/dist/data-structures/queue/deque.d.ts +69 -0
  53. package/dist/data-structures/queue/deque.js +69 -0
  54. package/dist/data-structures/queue/queue.d.ts +30 -32
  55. package/dist/data-structures/queue/queue.js +30 -32
  56. package/dist/data-structures/stack/stack.d.ts +26 -31
  57. package/dist/data-structures/stack/stack.js +26 -31
  58. package/dist/data-structures/trie/trie.d.ts +26 -6
  59. package/dist/data-structures/trie/trie.js +26 -7
  60. package/dist/data-structures/types/avl-tree.d.ts +1 -1
  61. package/dist/data-structures/types/binary-tree.d.ts +1 -1
  62. package/dist/data-structures/types/index.d.ts +1 -1
  63. package/dist/data-structures/types/index.js +1 -1
  64. package/dist/data-structures/types/singly-linked-list.d.ts +1 -5
  65. package/dist/data-structures/types/tree-multiset.d.ts +1 -1
  66. package/dist/{data-structures → utils}/trampoline.d.ts +5 -7
  67. package/dist/utils/types/index.d.ts +1 -0
  68. package/dist/utils/types/index.js +17 -0
  69. package/dist/{data-structures → utils}/types/utils.d.ts +8 -0
  70. package/dist/{data-structures → utils}/types/utils.js +1 -0
  71. package/dist/utils/utils.d.ts +1 -1
  72. package/docs/.nojekyll +1 -0
  73. package/docs/assets/highlight.css +85 -0
  74. package/docs/assets/main.js +58 -0
  75. package/docs/assets/search.js +1 -0
  76. package/docs/assets/style.css +1367 -0
  77. package/docs/classes/AVLTree.html +2046 -0
  78. package/docs/classes/AVLTreeNode.html +423 -0
  79. package/docs/classes/AaTree.html +117 -0
  80. package/docs/classes/AbstractEdge.html +198 -0
  81. package/docs/classes/AbstractGraph.html +891 -0
  82. package/docs/classes/AbstractVertex.html +164 -0
  83. package/docs/classes/ArrayDeque.html +384 -0
  84. package/docs/classes/BST.html +1893 -0
  85. package/docs/classes/BSTNode.html +425 -0
  86. package/docs/classes/BTree.html +117 -0
  87. package/docs/classes/BinaryIndexedTree.html +244 -0
  88. package/docs/classes/BinaryTree.html +1754 -0
  89. package/docs/classes/BinaryTreeNode.html +396 -0
  90. package/docs/classes/Character.html +165 -0
  91. package/docs/classes/CoordinateMap.html +394 -0
  92. package/docs/classes/CoordinateSet.html +355 -0
  93. package/docs/classes/Deque.html +617 -0
  94. package/docs/classes/DirectedEdge.html +247 -0
  95. package/docs/classes/DirectedGraph.html +1207 -0
  96. package/docs/classes/DirectedVertex.html +154 -0
  97. package/docs/classes/DoublyLinkedList.html +619 -0
  98. package/docs/classes/DoublyLinkedListNode.html +160 -0
  99. package/docs/classes/Heap.html +315 -0
  100. package/docs/classes/Matrix2D.html +447 -0
  101. package/docs/classes/MatrixNTI2D.html +181 -0
  102. package/docs/classes/MaxHeap.html +325 -0
  103. package/docs/classes/MaxPriorityQueue.html +668 -0
  104. package/docs/classes/MinHeap.html +326 -0
  105. package/docs/classes/MinPriorityQueue.html +668 -0
  106. package/docs/classes/Navigator.html +285 -0
  107. package/docs/classes/ObjectDeque.html +289 -0
  108. package/docs/classes/PriorityQueue.html +643 -0
  109. package/docs/classes/Queue.html +337 -0
  110. package/docs/classes/RBTree.html +117 -0
  111. package/docs/classes/SegmentTree.html +234 -0
  112. package/docs/classes/SegmentTreeNode.html +302 -0
  113. package/docs/classes/SinglyLinkedList.html +1035 -0
  114. package/docs/classes/SinglyLinkedListNode.html +304 -0
  115. package/docs/classes/SplayTree.html +117 -0
  116. package/docs/classes/Stack.html +313 -0
  117. package/docs/classes/TreeMultiSet.html +1897 -0
  118. package/docs/classes/Trie.html +317 -0
  119. package/docs/classes/TrieNode.html +221 -0
  120. package/docs/classes/TwoThreeTree.html +117 -0
  121. package/docs/classes/UndirectedEdge.html +220 -0
  122. package/docs/classes/UndirectedGraph.html +1006 -0
  123. package/docs/classes/UndirectedVertex.html +154 -0
  124. package/docs/classes/Vector2D.html +746 -0
  125. package/docs/enums/CP.html +126 -0
  126. package/docs/enums/FamilyPosition.html +126 -0
  127. package/docs/enums/LoopType.html +119 -0
  128. package/docs/index.html +288 -0
  129. package/docs/modules.html +146 -0
  130. package/jest.config.js +5 -0
  131. package/package.json +19 -11
  132. package/rename_clear_files.sh +29 -0
  133. package/src/assets/complexities-diff.jpg +0 -0
  134. package/src/assets/data-structure-complexities.jpg +0 -0
  135. package/src/data-structures/binary-tree/avl-tree.ts +57 -0
  136. package/src/data-structures/binary-tree/binary-indexed-tree.ts +27 -0
  137. package/src/data-structures/binary-tree/binary-tree.ts +288 -18
  138. package/src/data-structures/binary-tree/bst.ts +16 -1
  139. package/src/data-structures/binary-tree/segment-tree.ts +5 -0
  140. package/src/data-structures/binary-tree/tree-multiset.ts +4 -0
  141. package/src/data-structures/diagrams/README.md +5 -7
  142. package/src/data-structures/graph/abstract-graph.ts +167 -15
  143. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  144. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  145. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  146. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  147. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  148. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  149. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  150. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  151. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  152. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  153. package/src/data-structures/graph/directed-graph.ts +127 -5
  154. package/src/data-structures/graph/undirected-graph.ts +67 -1
  155. package/src/data-structures/hash/coordinate-map.ts +32 -0
  156. package/src/data-structures/hash/coordinate-set.ts +25 -0
  157. package/src/data-structures/heap/heap.ts +27 -32
  158. package/src/data-structures/heap/max-heap.ts +6 -1
  159. package/src/data-structures/heap/min-heap.ts +7 -1
  160. package/src/data-structures/index.ts +0 -1
  161. package/src/data-structures/linked-list/doubly-linked-list.ts +49 -8
  162. package/src/data-structures/linked-list/singly-linked-list.ts +39 -8
  163. package/src/data-structures/matrix/matrix.ts +11 -0
  164. package/src/data-structures/matrix/matrix2d.ts +82 -2
  165. package/src/data-structures/matrix/navigator.ts +32 -0
  166. package/src/data-structures/matrix/vector2d.ts +139 -15
  167. package/src/data-structures/priority-queue/max-priority-queue.ts +8 -0
  168. package/src/data-structures/priority-queue/min-priority-queue.ts +8 -0
  169. package/src/data-structures/priority-queue/priority-queue.ts +130 -0
  170. package/src/data-structures/queue/deque.ts +69 -0
  171. package/src/data-structures/queue/queue.ts +30 -32
  172. package/src/data-structures/stack/stack.ts +26 -31
  173. package/src/data-structures/trie/trie.ts +26 -7
  174. package/src/data-structures/types/avl-tree.ts +1 -1
  175. package/src/data-structures/types/binary-tree.ts +1 -1
  176. package/src/data-structures/types/bst.ts +1 -1
  177. package/src/data-structures/types/index.ts +1 -1
  178. package/src/data-structures/types/navigator.ts +1 -0
  179. package/src/data-structures/types/singly-linked-list.ts +1 -15
  180. package/src/data-structures/types/tree-multiset.ts +1 -1
  181. package/src/{data-structures → utils}/trampoline.ts +6 -6
  182. package/src/utils/types/index.ts +1 -0
  183. package/src/{data-structures → utils}/types/utils.ts +7 -0
  184. package/src/utils/utils.ts +12 -2
  185. package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
  186. package/tests/unit/data-structures/graph/abstract-graph.ts +0 -0
  187. package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
  188. package/tests/unit/data-structures/graph/index.ts +3 -0
  189. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  191. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  192. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  194. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  195. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  196. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  197. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  198. package/src/data-structures/graph/diagrams/mst.png +0 -0
  199. /package/dist/{data-structures → utils}/trampoline.js +0 -0
@@ -0,0 +1,29 @@
1
+ #!/bin/bash
2
+
3
+ # iterate the directory to rename and clear content of files
4
+ rename_and_clear_files() {
5
+ local directory="$1"
6
+ local extension="$2"
7
+ local new_prefix="$3"
8
+
9
+ for file in "$directory"/*; do
10
+ if [ -d "$file" ]; then
11
+ rename_and_clear_files "$file" "$extension" "$new_prefix"
12
+ elif [ -f "$file" ] && [[ "$file" == *"$extension" ]]; then
13
+ filename=$(basename "$file" "$extension")
14
+ new_name="$directory/${filename}${new_prefix}${extension}"
15
+ mv "$file" "$new_name"
16
+ echo "Renamed $file to $new_name"
17
+ > "$new_name" # clear content of file
18
+ fi
19
+ done
20
+ }
21
+
22
+ # check if directory, extension and prefix were provided
23
+ if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then
24
+ echo "Usage: $0 <directory> <extension> <new_prefix>"
25
+ exit 1
26
+ fi
27
+
28
+ # evoke the recursive function to rename and clear files
29
+ rename_and_clear_files "$1" "$2" "$3"
Binary file
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  import {BST, BSTNode} from './bst';
2
6
  import type {AVLTreeDeleted, BinaryTreeNodeId} from '../types';
3
7
 
@@ -13,12 +17,34 @@ export class AVLTree<T> extends BST<T> {
13
17
  return new AVLTreeNode<T>(id, val, count);
14
18
  }
15
19
 
20
+ /**
21
+ * The function overrides the put method of a Binary Search Tree to insert a node with a given id and value, and then
22
+ * balances the tree.
23
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to put or
24
+ * update in the AVL tree.
25
+ * @param {T | null} val - The `val` parameter represents the value that you want to assign to the node with the given
26
+ * `id`. It can be of type `T` (the generic type) or `null`.
27
+ * @param {number} [count] - The `count` parameter is an optional parameter of type `number`. It represents the number
28
+ * of times the value `val` should be inserted into the AVL tree. If the `count` parameter is not provided, it defaults
29
+ * to `1`, indicating that the value should be inserted once.
30
+ * @returns The method is returning either an AVLTreeNode<T> object or null.
31
+ */
16
32
  override put(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode<T> | null {
17
33
  const inserted = super.put(id, val, count);
18
34
  if (inserted) this.balancePath(inserted);
19
35
  return inserted;
20
36
  }
21
37
 
38
+ /**
39
+ * The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
40
+ * then balances the tree if necessary.
41
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
42
+ * removed from the AVL tree.
43
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
44
+ * determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
45
+ * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
46
+ * @returns The method is returning an array of `AVLTreeDeleted<T>` objects.
47
+ */
22
48
  override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): AVLTreeDeleted<T>[] {
23
49
  const deletedResults = super.remove(id, isUpdateAllLeftSum);
24
50
  for (const {needBalanced} of deletedResults) {
@@ -29,6 +55,12 @@ export class AVLTree<T> extends BST<T> {
29
55
  return deletedResults;
30
56
  }
31
57
 
58
+ /**
59
+ * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
60
+ * height of its right subtree.
61
+ * @param node - The parameter "node" is of type AVLTreeNode<T>, which represents a node in an AVL tree.
62
+ * @returns The balance factor of the given AVL tree node.
63
+ */
32
64
  balanceFactor(node: AVLTreeNode<T>): number {
33
65
  if (!node.right) // node has no right subtree
34
66
  return -node.height;
@@ -38,6 +70,10 @@ export class AVLTree<T> extends BST<T> {
38
70
  return node.right.height - node.left.height;
39
71
  }
40
72
 
73
+ /**
74
+ * The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
75
+ * @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
76
+ */
41
77
  updateHeight(node: AVLTreeNode<T>): void {
42
78
  if (!node.left && !node.right) // node is a leaf
43
79
  node.height = 0;
@@ -51,6 +87,11 @@ export class AVLTree<T> extends BST<T> {
51
87
  node.height = 1 + Math.max(node.right.height, node.left.height);
52
88
  }
53
89
 
90
+ /**
91
+ * The `balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
92
+ * each node in the path from the given node to the root.
93
+ * @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
94
+ */
54
95
  balancePath(node: AVLTreeNode<T>): void {
55
96
  const path = this.getPathToRoot(node);
56
97
  for (let i = path.length - 1; i >= 0; i--) {
@@ -78,6 +119,10 @@ export class AVLTree<T> extends BST<T> {
78
119
  }
79
120
  }
80
121
 
122
+ /**
123
+ * The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
124
+ * @param A - The parameter A is an AVLTreeNode object.
125
+ */
81
126
  balanceLL(A: AVLTreeNode<T>): void {
82
127
  const parentOfA = A.parent;
83
128
  const B = A.left; // A is left-heavy and B is left-heavy
@@ -104,6 +149,10 @@ export class AVLTree<T> extends BST<T> {
104
149
  if (B) this.updateHeight(B);
105
150
  }
106
151
 
152
+ /**
153
+ * The `balanceLR` function performs a left-right rotation to balance an AVL tree.
154
+ * @param A - A is an AVLTreeNode object.
155
+ */
107
156
  balanceLR(A: AVLTreeNode<T>): void {
108
157
  const parentOfA = A.parent;
109
158
  const B = A.left; // A is left-heavy
@@ -148,6 +197,10 @@ export class AVLTree<T> extends BST<T> {
148
197
  C && this.updateHeight(C);
149
198
  }
150
199
 
200
+ /**
201
+ * The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
202
+ * @param A - The parameter A is an AVLTreeNode object.
203
+ */
151
204
  balanceRR(A: AVLTreeNode<T>): void {
152
205
  const parentOfA = A.parent;
153
206
  const B = A.right; // A is right-heavy and B is right-heavy
@@ -179,6 +232,10 @@ export class AVLTree<T> extends BST<T> {
179
232
  B && this.updateHeight(B);
180
233
  }
181
234
 
235
+ /**
236
+ * The `balanceRL` function performs a right-left rotation to balance an AVL tree.
237
+ * @param A - A is an AVLTreeNode object.
238
+ */
182
239
  balanceRL(A: AVLTreeNode<T>): void {
183
240
  const parentOfA = A.parent;
184
241
  const B = A.right; // A is right-heavy
@@ -1,3 +1,7 @@
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
1
5
  export class BinaryIndexedTree {
2
6
  private readonly _sumTree: number[];
3
7
 
@@ -9,6 +13,14 @@ export class BinaryIndexedTree {
9
13
  return x & (-x);
10
14
  }
11
15
 
16
+ /**
17
+ * The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
18
+ * its ancestors.
19
+ * @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
20
+ * updated.
21
+ * @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
22
+ * at index "i" in the "_sumTree" array.
23
+ */
12
24
  update(i: number, delta: number) {
13
25
  while (i < this._sumTree.length) {
14
26
  this._sumTree[i] += delta;
@@ -16,6 +28,13 @@ export class BinaryIndexedTree {
16
28
  }
17
29
  }
18
30
 
31
+ /**
32
+ * The function calculates the prefix sum of an array using a binary indexed tree.
33
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
34
+ * array for which we want to calculate the prefix sum.
35
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
36
+ * `i`.
37
+ */
19
38
  getPrefixSum(i: number) {
20
39
  let sum = 0;
21
40
  while (i > 0) {
@@ -25,6 +44,14 @@ export class BinaryIndexedTree {
25
44
  return sum;
26
45
  }
27
46
 
47
+ /**
48
+ * The function `getRangeSum` calculates the sum of a range of numbers in an array.
49
+ * @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
50
+ * sum.
51
+ * @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
52
+ * the sum.
53
+ * @returns the sum of the elements in the range specified by the start and end indices.
54
+ */
28
55
  public getRangeSum(start: number, end: number): number {
29
56
  if (!(0 <= start && start <= end && end <= this._sumTree.length))
30
57
  throw 'Index out of bounds';