data-structure-typed 0.9.16 → 1.3.1

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 (264) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +665 -172
  3. package/dist/bundle.js +2 -0
  4. package/dist/bundle.js.LICENSE.txt +13 -0
  5. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  6. package/dist/data-structures/binary-tree/abstract-binary-tree.d.ts +364 -0
  7. package/dist/data-structures/binary-tree/abstract-binary-tree.js +1308 -0
  8. package/dist/data-structures/binary-tree/avl-tree.d.ts +85 -14
  9. package/dist/data-structures/binary-tree/avl-tree.js +142 -116
  10. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +39 -1
  12. package/dist/data-structures/binary-tree/binary-indexed-tree.js +54 -13
  13. package/dist/data-structures/binary-tree/binary-tree.d.ts +29 -126
  14. package/dist/data-structures/binary-tree/binary-tree.js +31 -1093
  15. package/dist/data-structures/binary-tree/bst.d.ts +117 -23
  16. package/dist/data-structures/binary-tree/bst.js +233 -240
  17. package/dist/data-structures/binary-tree/index.d.ts +1 -0
  18. package/dist/data-structures/binary-tree/index.js +1 -0
  19. package/dist/data-structures/binary-tree/rb-tree.d.ts +18 -1
  20. package/dist/data-structures/binary-tree/rb-tree.js +40 -5
  21. package/dist/data-structures/binary-tree/segment-tree.d.ts +61 -11
  22. package/dist/data-structures/binary-tree/segment-tree.js +126 -93
  23. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  24. package/dist/data-structures/binary-tree/tree-multiset.d.ts +213 -6
  25. package/dist/data-structures/binary-tree/tree-multiset.js +687 -34
  26. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  27. package/dist/data-structures/graph/abstract-graph.d.ts +270 -36
  28. package/dist/data-structures/graph/abstract-graph.js +610 -572
  29. package/dist/data-structures/graph/directed-graph.d.ts +173 -16
  30. package/dist/data-structures/graph/directed-graph.js +345 -313
  31. package/dist/data-structures/graph/index.d.ts +1 -0
  32. package/dist/data-structures/graph/index.js +1 -0
  33. package/dist/data-structures/graph/map-graph.d.ts +79 -0
  34. package/dist/data-structures/graph/map-graph.js +111 -0
  35. package/dist/data-structures/graph/undirected-graph.d.ts +111 -9
  36. package/dist/data-structures/graph/undirected-graph.js +203 -178
  37. package/dist/data-structures/hash/coordinate-map.d.ts +38 -1
  38. package/dist/data-structures/hash/coordinate-map.js +59 -36
  39. package/dist/data-structures/hash/coordinate-set.d.ts +32 -2
  40. package/dist/data-structures/hash/coordinate-set.js +49 -33
  41. package/dist/data-structures/hash/hash-table.d.ts +2 -1
  42. package/dist/data-structures/hash/hash-table.js +4 -0
  43. package/dist/data-structures/hash/pair.d.ts +2 -1
  44. package/dist/data-structures/hash/pair.js +4 -0
  45. package/dist/data-structures/hash/tree-map.d.ts +2 -1
  46. package/dist/data-structures/hash/tree-map.js +4 -0
  47. package/dist/data-structures/hash/tree-set.d.ts +2 -1
  48. package/dist/data-structures/hash/tree-set.js +4 -0
  49. package/dist/data-structures/heap/heap.d.ts +62 -45
  50. package/dist/data-structures/heap/heap.js +124 -86
  51. package/dist/data-structures/heap/max-heap.d.ts +13 -5
  52. package/dist/data-structures/heap/max-heap.js +18 -28
  53. package/dist/data-structures/heap/min-heap.d.ts +14 -5
  54. package/dist/data-structures/heap/min-heap.js +19 -28
  55. package/dist/data-structures/index.d.ts +1 -1
  56. package/dist/data-structures/index.js +1 -1
  57. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +193 -56
  58. package/dist/data-structures/linked-list/doubly-linked-list.js +484 -220
  59. package/dist/data-structures/linked-list/index.d.ts +1 -0
  60. package/dist/data-structures/linked-list/index.js +1 -0
  61. package/dist/data-structures/linked-list/singly-linked-list.d.ts +117 -315
  62. package/dist/data-structures/linked-list/singly-linked-list.js +374 -727
  63. package/dist/data-structures/linked-list/skip-linked-list.d.ts +2 -1
  64. package/dist/data-structures/linked-list/skip-linked-list.js +4 -0
  65. package/dist/data-structures/matrix/matrix.d.ts +12 -0
  66. package/dist/data-structures/matrix/matrix.js +21 -8
  67. package/dist/data-structures/matrix/matrix2d.d.ts +85 -2
  68. package/dist/data-structures/matrix/matrix2d.js +146 -80
  69. package/dist/data-structures/matrix/navigator.d.ts +36 -1
  70. package/dist/data-structures/matrix/navigator.js +46 -37
  71. package/dist/data-structures/matrix/vector2d.d.ts +142 -15
  72. package/dist/data-structures/matrix/vector2d.js +215 -109
  73. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +12 -2
  74. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -26
  75. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +12 -2
  76. package/dist/data-structures/priority-queue/min-priority-queue.js +34 -26
  77. package/dist/data-structures/priority-queue/priority-queue.d.ts +153 -3
  78. package/dist/data-structures/priority-queue/priority-queue.js +244 -143
  79. package/dist/data-structures/queue/deque.d.ts +141 -13
  80. package/dist/data-structures/queue/deque.js +200 -82
  81. package/dist/data-structures/queue/queue.d.ts +65 -38
  82. package/dist/data-structures/queue/queue.js +110 -66
  83. package/dist/data-structures/stack/stack.d.ts +27 -32
  84. package/dist/data-structures/stack/stack.js +47 -53
  85. package/dist/data-structures/tree/index.d.ts +1 -0
  86. package/dist/data-structures/tree/index.js +17 -0
  87. package/dist/data-structures/tree/tree.d.ts +14 -0
  88. package/dist/data-structures/tree/tree.js +60 -0
  89. package/dist/data-structures/trie/trie.d.ts +33 -10
  90. package/dist/data-structures/trie/trie.js +123 -208
  91. package/dist/index.d.ts +3 -0
  92. package/dist/index.js +3 -0
  93. package/dist/interfaces/abstract-binary-tree.d.ts +90 -0
  94. package/dist/interfaces/abstract-graph.d.ts +17 -0
  95. package/dist/interfaces/avl-tree.d.ts +9 -0
  96. package/dist/interfaces/binary-tree.d.ts +6 -0
  97. package/dist/interfaces/bst.d.ts +17 -0
  98. package/dist/interfaces/directed-graph.d.ts +12 -0
  99. package/{src/data-structures/types/index.ts → dist/interfaces/index.d.ts} +10 -8
  100. package/dist/interfaces/index.js +31 -0
  101. package/{src/data-structures/hash/hash-table.ts → dist/interfaces/priority-queue.d.ts} +1 -1
  102. package/dist/interfaces/rb-tree.d.ts +8 -0
  103. package/dist/interfaces/segment-tree.d.ts +1 -0
  104. package/dist/interfaces/singly-linked-list.d.ts +1 -0
  105. package/dist/interfaces/singly-linked-list.js +2 -0
  106. package/dist/interfaces/tree-multiset.d.ts +7 -0
  107. package/dist/interfaces/tree-multiset.js +2 -0
  108. package/dist/interfaces/undirected-graph.d.ts +5 -0
  109. package/dist/interfaces/undirected-graph.js +2 -0
  110. package/dist/types/data-structures/abstract-binary-tree.d.ts +34 -0
  111. package/dist/types/data-structures/abstract-binary-tree.js +25 -0
  112. package/dist/types/data-structures/abstract-graph.d.ts +11 -0
  113. package/dist/types/data-structures/abstract-graph.js +2 -0
  114. package/dist/types/data-structures/avl-tree.d.ts +4 -0
  115. package/dist/types/data-structures/avl-tree.js +2 -0
  116. package/dist/types/data-structures/binary-tree.d.ts +4 -0
  117. package/dist/types/data-structures/binary-tree.js +2 -0
  118. package/dist/types/data-structures/bst.d.ts +13 -0
  119. package/dist/types/data-structures/bst.js +9 -0
  120. package/dist/types/data-structures/directed-graph.d.ts +6 -0
  121. package/dist/types/data-structures/directed-graph.js +9 -0
  122. package/dist/types/data-structures/doubly-linked-list.d.ts +1 -0
  123. package/dist/types/data-structures/doubly-linked-list.js +2 -0
  124. package/dist/types/data-structures/heap.d.ts +3 -0
  125. package/dist/types/data-structures/heap.js +2 -0
  126. package/dist/{data-structures/types → types/data-structures}/index.d.ts +3 -1
  127. package/dist/{data-structures/types → types/data-structures}/index.js +3 -1
  128. package/dist/types/data-structures/map-graph.d.ts +1 -0
  129. package/dist/types/data-structures/map-graph.js +2 -0
  130. package/dist/{data-structures/types → types/data-structures}/navigator.d.ts +2 -2
  131. package/dist/types/data-structures/navigator.js +2 -0
  132. package/dist/{data-structures/types → types/data-structures}/priority-queue.d.ts +2 -2
  133. package/dist/types/data-structures/priority-queue.js +2 -0
  134. package/dist/types/data-structures/rb-tree.d.ts +8 -0
  135. package/dist/types/data-structures/rb-tree.js +8 -0
  136. package/dist/types/data-structures/segment-tree.js +2 -0
  137. package/dist/types/data-structures/singly-linked-list.d.ts +1 -0
  138. package/dist/types/data-structures/singly-linked-list.js +2 -0
  139. package/dist/types/data-structures/tree-multiset.d.ts +4 -0
  140. package/dist/types/data-structures/tree-multiset.js +2 -0
  141. package/dist/types/helpers.d.ts +1 -0
  142. package/dist/types/helpers.js +2 -0
  143. package/dist/types/index.d.ts +3 -0
  144. package/dist/types/index.js +19 -0
  145. package/dist/types/utils/index.d.ts +2 -0
  146. package/dist/types/utils/index.js +18 -0
  147. package/dist/types/utils/utils.d.ts +7 -0
  148. package/dist/types/utils/utils.js +2 -0
  149. package/dist/types/utils/validate-type.d.ts +19 -0
  150. package/dist/types/utils/validate-type.js +2 -0
  151. package/dist/utils/utils.d.ts +17 -103
  152. package/dist/utils/utils.js +40 -625
  153. package/package.json +134 -23
  154. package/.idea/data-structure-typed.iml +0 -12
  155. package/.idea/modules.xml +0 -8
  156. package/.idea/vcs.xml +0 -6
  157. package/dist/data-structures/trampoline.d.ts +0 -16
  158. package/dist/data-structures/trampoline.js +0 -130
  159. package/dist/data-structures/types/abstract-graph.d.ts +0 -29
  160. package/dist/data-structures/types/avl-tree.d.ts +0 -5
  161. package/dist/data-structures/types/binary-tree.d.ts +0 -16
  162. package/dist/data-structures/types/bst.d.ts +0 -7
  163. package/dist/data-structures/types/directed-graph.d.ts +0 -10
  164. package/dist/data-structures/types/doubly-linked-list.d.ts +0 -1
  165. package/dist/data-structures/types/heap.d.ts +0 -7
  166. package/dist/data-structures/types/singly-linked-list.d.ts +0 -5
  167. package/dist/data-structures/types/tree-multiset.d.ts +0 -5
  168. package/dist/data-structures/types/utils.d.ts +0 -52
  169. package/dist/data-structures/types/utils.js +0 -54
  170. package/src/data-structures/binary-tree/aa-tree.ts +0 -3
  171. package/src/data-structures/binary-tree/avl-tree.ts +0 -227
  172. package/src/data-structures/binary-tree/b-tree.ts +0 -3
  173. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -33
  174. package/src/data-structures/binary-tree/binary-tree.ts +0 -1133
  175. package/src/data-structures/binary-tree/bst.ts +0 -395
  176. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  177. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  178. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  179. package/src/data-structures/binary-tree/index.ts +0 -11
  180. package/src/data-structures/binary-tree/rb-tree.ts +0 -3
  181. package/src/data-structures/binary-tree/segment-tree.ts +0 -172
  182. package/src/data-structures/binary-tree/splay-tree.ts +0 -3
  183. package/src/data-structures/binary-tree/tree-multiset.ts +0 -18
  184. package/src/data-structures/binary-tree/two-three-tree.ts +0 -3
  185. package/src/data-structures/diagrams/README.md +0 -7
  186. package/src/data-structures/graph/abstract-graph.ts +0 -753
  187. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.png +0 -0
  188. package/src/data-structures/graph/diagrams/adjacency-list.png +0 -0
  189. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.png +0 -0
  190. package/src/data-structures/graph/diagrams/adjacency-matrix.png +0 -0
  191. package/src/data-structures/graph/diagrams/dfs-can-do.png +0 -0
  192. package/src/data-structures/graph/diagrams/edge-list-pros-cons.png +0 -0
  193. package/src/data-structures/graph/diagrams/edge-list.png +0 -0
  194. package/src/data-structures/graph/diagrams/max-flow.png +0 -0
  195. package/src/data-structures/graph/diagrams/mst.png +0 -0
  196. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  197. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  198. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  199. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  200. package/src/data-structures/graph/directed-graph.ts +0 -306
  201. package/src/data-structures/graph/index.ts +0 -3
  202. package/src/data-structures/graph/undirected-graph.ts +0 -155
  203. package/src/data-structures/hash/coordinate-map.ts +0 -24
  204. package/src/data-structures/hash/coordinate-set.ts +0 -20
  205. package/src/data-structures/hash/index.ts +0 -6
  206. package/src/data-structures/heap/heap.ts +0 -127
  207. package/src/data-structures/heap/index.ts +0 -3
  208. package/src/data-structures/heap/max-heap.ts +0 -23
  209. package/src/data-structures/heap/min-heap.ts +0 -25
  210. package/src/data-structures/index.ts +0 -12
  211. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -250
  212. package/src/data-structures/linked-list/index.ts +0 -2
  213. package/src/data-structures/linked-list/singly-linked-list.ts +0 -736
  214. package/src/data-structures/linked-list/skip-linked-list.ts +0 -1
  215. package/src/data-structures/matrix/index.ts +0 -4
  216. package/src/data-structures/matrix/matrix.ts +0 -13
  217. package/src/data-structures/matrix/matrix2d.ts +0 -125
  218. package/src/data-structures/matrix/navigator.ts +0 -87
  219. package/src/data-structures/matrix/vector2d.ts +0 -189
  220. package/src/data-structures/priority-queue/index.ts +0 -3
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -13
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -13
  223. package/src/data-structures/priority-queue/priority-queue.ts +0 -200
  224. package/src/data-structures/queue/deque.ts +0 -139
  225. package/src/data-structures/queue/index.ts +0 -2
  226. package/src/data-structures/queue/queue.ts +0 -122
  227. package/src/data-structures/stack/index.ts +0 -1
  228. package/src/data-structures/stack/stack.ts +0 -103
  229. package/src/data-structures/trampoline.ts +0 -51
  230. package/src/data-structures/trie/index.ts +0 -1
  231. package/src/data-structures/trie/trie.ts +0 -203
  232. package/src/data-structures/types/abstract-graph.ts +0 -51
  233. package/src/data-structures/types/avl-tree.ts +0 -6
  234. package/src/data-structures/types/binary-tree.ts +0 -15
  235. package/src/data-structures/types/bst.ts +0 -5
  236. package/src/data-structures/types/directed-graph.ts +0 -18
  237. package/src/data-structures/types/doubly-linked-list.ts +0 -1
  238. package/src/data-structures/types/heap.ts +0 -8
  239. package/src/data-structures/types/navigator.ts +0 -12
  240. package/src/data-structures/types/priority-queue.ts +0 -9
  241. package/src/data-structures/types/segment-tree.ts +0 -1
  242. package/src/data-structures/types/singly-linked-list.ts +0 -15
  243. package/src/data-structures/types/tree-multiset.ts +0 -3
  244. package/src/data-structures/types/utils.ts +0 -173
  245. package/src/index.ts +0 -1
  246. package/src/utils/index.ts +0 -1
  247. package/src/utils/utils.ts +0 -505
  248. package/tsconfig.json +0 -56
  249. /package/dist/{data-structures/types/abstract-graph.js → interfaces/abstract-binary-tree.js} +0 -0
  250. /package/dist/{data-structures/types/avl-tree.js → interfaces/abstract-graph.js} +0 -0
  251. /package/dist/{data-structures/types/binary-tree.js → interfaces/avl-tree.js} +0 -0
  252. /package/dist/{data-structures/types/bst.js → interfaces/binary-tree.js} +0 -0
  253. /package/dist/{data-structures/types/directed-graph.js → interfaces/bst.js} +0 -0
  254. /package/dist/{data-structures/types/doubly-linked-list.js → interfaces/directed-graph.js} +0 -0
  255. /package/{src/data-structures/hash/pair.ts → dist/interfaces/doubly-linked-list.d.ts} +0 -0
  256. /package/dist/{data-structures/types/heap.js → interfaces/doubly-linked-list.js} +0 -0
  257. /package/{src/data-structures/hash/tree-map.ts → dist/interfaces/heap.d.ts} +0 -0
  258. /package/dist/{data-structures/types/navigator.js → interfaces/heap.js} +0 -0
  259. /package/{src/data-structures/hash/tree-set.ts → dist/interfaces/navigator.d.ts} +0 -0
  260. /package/dist/{data-structures/types/priority-queue.js → interfaces/navigator.js} +0 -0
  261. /package/dist/{data-structures/types/segment-tree.js → interfaces/priority-queue.js} +0 -0
  262. /package/dist/{data-structures/types/singly-linked-list.js → interfaces/rb-tree.js} +0 -0
  263. /package/dist/{data-structures/types/tree-multiset.js → interfaces/segment-tree.js} +0 -0
  264. /package/dist/{data-structures/types → types/data-structures}/segment-tree.d.ts +0 -0
@@ -1,17 +1,88 @@
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
+ */
1
8
  import { BST, BSTNode } from './bst';
2
- import type { AVLTreeDeleted, BinaryTreeNodeId } from '../types';
3
- export declare class AVLTreeNode<T> extends BSTNode<T> {
4
- clone(): AVLTreeNode<T>;
9
+ import type { AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId } from '../../types';
10
+ import { IAVLTree, IAVLTreeNode } from '../../interfaces';
11
+ export declare class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
12
+ constructor(id: BinaryTreeNodeId, val?: T);
5
13
  }
6
- export declare class AVLTree<T> extends BST<T> {
7
- createNode(id: BinaryTreeNodeId, val: T, count?: number): AVLTreeNode<T>;
8
- put(id: BinaryTreeNodeId, val: T | null, count?: number): AVLTreeNode<T> | null;
9
- remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): AVLTreeDeleted<T>[];
10
- balanceFactor(node: AVLTreeNode<T>): number;
11
- updateHeight(node: AVLTreeNode<T>): void;
12
- balancePath(node: AVLTreeNode<T>): void;
13
- balanceLL(A: AVLTreeNode<T>): void;
14
- balanceLR(A: AVLTreeNode<T>): void;
15
- balanceRR(A: AVLTreeNode<T>): void;
16
- balanceRL(A: AVLTreeNode<T>): void;
14
+ export declare class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
15
+ /**
16
+ * This is a constructor function for an AVL tree data structure in TypeScript.
17
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
18
+ * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
19
+ * options.
20
+ */
21
+ constructor(options?: AVLTreeOptions);
22
+ /**
23
+ * The function creates a new AVL tree node with the given id and value.
24
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
25
+ * identify each node in the tree.
26
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
27
+ * that will be stored in the node.
28
+ * @returns a new AVLTreeNode object with the specified id and value.
29
+ */
30
+ createNode(id: BinaryTreeNodeId, val?: N['val']): N;
31
+ /**
32
+ * The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
33
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
34
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
35
+ * `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
36
+ * @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
37
+ */
38
+ add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined;
39
+ /**
40
+ * The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
41
+ * then balances the tree if necessary.
42
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
43
+ * removed from the AVL tree.
44
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
45
+ * determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
46
+ * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
47
+ * @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
48
+ */
49
+ remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[];
50
+ /**
51
+ * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
52
+ * height of its right subtree.
53
+ * @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
54
+ * @returns The balance factor of the given AVL tree node.
55
+ */
56
+ protected _balanceFactor(node: N): number;
57
+ /**
58
+ * The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
59
+ * @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
60
+ */
61
+ protected _updateHeight(node: N): void;
62
+ /**
63
+ * The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
64
+ * each node in the path from the given node to the root.
65
+ * @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
66
+ */
67
+ protected _balancePath(node: N): void;
68
+ /**
69
+ * The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
70
+ * @param A - The parameter A is an AVLTreeNode object.
71
+ */
72
+ protected _balanceLL(A: N): void;
73
+ /**
74
+ * The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
75
+ * @param A - A is an AVLTreeNode object.
76
+ */
77
+ protected _balanceLR(A: N): void;
78
+ /**
79
+ * The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
80
+ * @param A - The parameter A is an AVLTreeNode object.
81
+ */
82
+ protected _balanceRR(A: N): void;
83
+ /**
84
+ * The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
85
+ * @param A - A is an AVLTreeNode object.
86
+ */
87
+ protected _balanceRL(A: N): void;
17
88
  }
@@ -1,130 +1,145 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __values = (this && this.__values) || function(o) {
18
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
19
- if (m) return m.call(o);
20
- if (o && typeof o.length === "number") return {
21
- next: function () {
22
- if (o && i >= o.length) o = void 0;
23
- return { value: o && o[i++], done: !o };
24
- }
25
- };
26
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
27
- };
28
2
  Object.defineProperty(exports, "__esModule", { value: true });
29
3
  exports.AVLTree = exports.AVLTreeNode = void 0;
30
- var bst_1 = require("./bst");
31
- var AVLTreeNode = /** @class */ (function (_super) {
32
- __extends(AVLTreeNode, _super);
33
- function AVLTreeNode() {
34
- return _super !== null && _super.apply(this, arguments) || this;
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
10
+ */
11
+ const bst_1 = require("./bst");
12
+ class AVLTreeNode extends bst_1.BSTNode {
13
+ constructor(id, val) {
14
+ super(id, val);
35
15
  }
36
- AVLTreeNode.prototype.clone = function () {
37
- return new AVLTreeNode(this.id, this.val, this.count);
38
- };
39
- return AVLTreeNode;
40
- }(bst_1.BSTNode));
16
+ }
41
17
  exports.AVLTreeNode = AVLTreeNode;
42
- var AVLTree = /** @class */ (function (_super) {
43
- __extends(AVLTree, _super);
44
- function AVLTree() {
45
- return _super !== null && _super.apply(this, arguments) || this;
18
+ class AVLTree extends bst_1.BST {
19
+ /**
20
+ * This is a constructor function for an AVL tree data structure in TypeScript.
21
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
22
+ * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
23
+ * options.
24
+ */
25
+ constructor(options) {
26
+ super(options);
27
+ }
28
+ /**
29
+ * The function creates a new AVL tree node with the given id and value.
30
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
31
+ * identify each node in the tree.
32
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
33
+ * that will be stored in the node.
34
+ * @returns a new AVLTreeNode object with the specified id and value.
35
+ */
36
+ createNode(id, val) {
37
+ return new AVLTreeNode(id, val);
46
38
  }
47
- AVLTree.prototype.createNode = function (id, val, count) {
48
- return new AVLTreeNode(id, val, count);
49
- };
50
- AVLTree.prototype.put = function (id, val, count) {
51
- var inserted = _super.prototype.put.call(this, id, val, count);
39
+ /**
40
+ * The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
41
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
42
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
43
+ * `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
44
+ * @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
45
+ */
46
+ add(id, val) {
47
+ // TODO support node as a param
48
+ const inserted = super.add(id, val);
52
49
  if (inserted)
53
- this.balancePath(inserted);
50
+ this._balancePath(inserted);
54
51
  return inserted;
55
- };
56
- AVLTree.prototype.remove = function (id, isUpdateAllLeftSum) {
57
- var e_1, _a;
58
- var deletedResults = _super.prototype.remove.call(this, id, isUpdateAllLeftSum);
59
- try {
60
- for (var deletedResults_1 = __values(deletedResults), deletedResults_1_1 = deletedResults_1.next(); !deletedResults_1_1.done; deletedResults_1_1 = deletedResults_1.next()) {
61
- var needBalanced = deletedResults_1_1.value.needBalanced;
62
- if (needBalanced) {
63
- this.balancePath(needBalanced);
64
- }
65
- }
66
- }
67
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
68
- finally {
69
- try {
70
- if (deletedResults_1_1 && !deletedResults_1_1.done && (_a = deletedResults_1.return)) _a.call(deletedResults_1);
52
+ }
53
+ /**
54
+ * The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
55
+ * then balances the tree if necessary.
56
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
57
+ * removed from the AVL tree.
58
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
59
+ * determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
60
+ * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
61
+ * @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
62
+ */
63
+ remove(id, isUpdateAllLeftSum) {
64
+ const deletedResults = super.remove(id, isUpdateAllLeftSum);
65
+ for (const { needBalanced } of deletedResults) {
66
+ if (needBalanced) {
67
+ this._balancePath(needBalanced);
71
68
  }
72
- finally { if (e_1) throw e_1.error; }
73
69
  }
74
70
  return deletedResults;
75
- };
76
- AVLTree.prototype.balanceFactor = function (node) {
71
+ }
72
+ /**
73
+ * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
74
+ * height of its right subtree.
75
+ * @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
76
+ * @returns The balance factor of the given AVL tree node.
77
+ */
78
+ _balanceFactor(node) {
77
79
  if (!node.right) // node has no right subtree
78
80
  return -node.height;
79
81
  else if (!node.left) // node has no left subtree
80
82
  return +node.height;
81
83
  else
82
84
  return node.right.height - node.left.height;
83
- };
84
- AVLTree.prototype.updateHeight = function (node) {
85
+ }
86
+ /**
87
+ * The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
88
+ * @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
89
+ */
90
+ _updateHeight(node) {
85
91
  if (!node.left && !node.right) // node is a leaf
86
92
  node.height = 0;
87
93
  else if (!node.left) {
88
94
  // node has no left subtree
89
- var rightHeight = node.right ? node.right.height : 0;
95
+ const rightHeight = node.right ? node.right.height : 0;
90
96
  node.height = 1 + rightHeight;
91
97
  }
92
98
  else if (!node.right) // node has no right subtree
93
99
  node.height = 1 + node.left.height;
94
100
  else
95
101
  node.height = 1 + Math.max(node.right.height, node.left.height);
96
- };
97
- AVLTree.prototype.balancePath = function (node) {
98
- var path = this.getPathToRoot(node);
99
- for (var i = path.length - 1; i >= 0; i--) {
100
- var A = path[i];
101
- this.updateHeight(A);
102
- switch (this.balanceFactor(A)) {
102
+ }
103
+ /**
104
+ * The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
105
+ * each node in the path from the given node to the root.
106
+ * @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
107
+ */
108
+ _balancePath(node) {
109
+ const path = this.getPathToRoot(node);
110
+ for (let i = path.length - 1; i >= 0; i--) {
111
+ const A = path[i];
112
+ this._updateHeight(A);
113
+ switch (this._balanceFactor(A)) {
103
114
  case -2:
104
115
  if (A && A.left) {
105
- if (this.balanceFactor(A.left) <= 0) {
106
- this.balanceLL(A); // Perform LL rotation
116
+ if (this._balanceFactor(A.left) <= 0) {
117
+ this._balanceLL(A); // Perform LL rotation
107
118
  }
108
119
  else {
109
- this.balanceLR(A); // Perform LR rotation
120
+ this._balanceLR(A); // Perform LR rotation
110
121
  }
111
122
  }
112
123
  break;
113
124
  case +2:
114
125
  if (A && A.right) {
115
- if (this.balanceFactor(A.right) >= 0) {
116
- this.balanceRR(A); // Perform RR rotation
126
+ if (this._balanceFactor(A.right) >= 0) {
127
+ this._balanceRR(A); // Perform RR rotation
117
128
  }
118
129
  else {
119
- this.balanceRL(A); // Perform RL rotation
130
+ this._balanceRL(A); // Perform RL rotation
120
131
  }
121
132
  }
122
133
  }
123
134
  }
124
- };
125
- AVLTree.prototype.balanceLL = function (A) {
126
- var parentOfA = A.parent;
127
- var B = A.left; // A is left-heavy and B is left-heavy
135
+ }
136
+ /**
137
+ * The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
138
+ * @param A - The parameter A is an AVLTreeNode object.
139
+ */
140
+ _balanceLL(A) {
141
+ const parentOfA = A.parent;
142
+ const B = A.left; // A is left-heavy and B is left-heavy
128
143
  A.parent = B;
129
144
  if (B && B.right) {
130
145
  B.right.parent = A;
@@ -133,7 +148,7 @@ var AVLTree = /** @class */ (function (_super) {
133
148
  B.parent = parentOfA;
134
149
  if (A === this.root) {
135
150
  if (B)
136
- this.root = B;
151
+ this._setRoot(B);
137
152
  }
138
153
  else {
139
154
  if ((parentOfA === null || parentOfA === void 0 ? void 0 : parentOfA.left) === A) {
@@ -148,14 +163,18 @@ var AVLTree = /** @class */ (function (_super) {
148
163
  A.left = B.right; // Make T2 the left subtree of A
149
164
  B.right = A; // Make A the left child of B
150
165
  }
151
- this.updateHeight(A);
166
+ this._updateHeight(A);
152
167
  if (B)
153
- this.updateHeight(B);
154
- };
155
- AVLTree.prototype.balanceLR = function (A) {
156
- var parentOfA = A.parent;
157
- var B = A.left; // A is left-heavy
158
- var C = null;
168
+ this._updateHeight(B);
169
+ }
170
+ /**
171
+ * The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
172
+ * @param A - A is an AVLTreeNode object.
173
+ */
174
+ _balanceLR(A) {
175
+ const parentOfA = A.parent;
176
+ const B = A.left; // A is left-heavy
177
+ let C = null;
159
178
  if (B) {
160
179
  C = B.right; // B is right-heavy
161
180
  }
@@ -174,7 +193,7 @@ var AVLTree = /** @class */ (function (_super) {
174
193
  }
175
194
  if (A === this.root) {
176
195
  if (C)
177
- this.root = C;
196
+ this._setRoot(C);
178
197
  }
179
198
  else {
180
199
  if (parentOfA) {
@@ -193,13 +212,17 @@ var AVLTree = /** @class */ (function (_super) {
193
212
  C.left = B;
194
213
  C.right = A;
195
214
  }
196
- this.updateHeight(A); // Adjust heights
197
- B && this.updateHeight(B);
198
- C && this.updateHeight(C);
199
- };
200
- AVLTree.prototype.balanceRR = function (A) {
201
- var parentOfA = A.parent;
202
- var B = A.right; // A is right-heavy and B is right-heavy
215
+ this._updateHeight(A); // Adjust heights
216
+ B && this._updateHeight(B);
217
+ C && this._updateHeight(C);
218
+ }
219
+ /**
220
+ * The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
221
+ * @param A - The parameter A is an AVLTreeNode object.
222
+ */
223
+ _balanceRR(A) {
224
+ const parentOfA = A.parent;
225
+ const B = A.right; // A is right-heavy and B is right-heavy
203
226
  A.parent = B;
204
227
  if (B) {
205
228
  if (B.left) {
@@ -209,7 +232,7 @@ var AVLTree = /** @class */ (function (_super) {
209
232
  }
210
233
  if (A === this.root) {
211
234
  if (B)
212
- this.root = B;
235
+ this._setRoot(B);
213
236
  }
214
237
  else {
215
238
  if (parentOfA) {
@@ -225,13 +248,17 @@ var AVLTree = /** @class */ (function (_super) {
225
248
  A.right = B.left; // Make T2 the right subtree of A
226
249
  B.left = A;
227
250
  }
228
- this.updateHeight(A);
229
- B && this.updateHeight(B);
230
- };
231
- AVLTree.prototype.balanceRL = function (A) {
232
- var parentOfA = A.parent;
233
- var B = A.right; // A is right-heavy
234
- var C = null;
251
+ this._updateHeight(A);
252
+ B && this._updateHeight(B);
253
+ }
254
+ /**
255
+ * The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
256
+ * @param A - A is an AVLTreeNode object.
257
+ */
258
+ _balanceRL(A) {
259
+ const parentOfA = A.parent;
260
+ const B = A.right; // A is right-heavy
261
+ let C = null;
235
262
  if (B) {
236
263
  C = B.left; // B is left-heavy
237
264
  }
@@ -249,7 +276,7 @@ var AVLTree = /** @class */ (function (_super) {
249
276
  }
250
277
  if (A === this.root) {
251
278
  if (C)
252
- this.root = C;
279
+ this._setRoot(C);
253
280
  }
254
281
  else {
255
282
  if (parentOfA) {
@@ -269,10 +296,9 @@ var AVLTree = /** @class */ (function (_super) {
269
296
  C.left = A;
270
297
  if (C)
271
298
  C.right = B;
272
- this.updateHeight(A); // Adjust heights
273
- B && this.updateHeight(B);
274
- C && this.updateHeight(C);
275
- };
276
- return AVLTree;
277
- }(bst_1.BST));
299
+ this._updateHeight(A); // Adjust heights
300
+ B && this._updateHeight(B);
301
+ C && this._updateHeight(C);
302
+ }
303
+ }
278
304
  exports.AVLTree = AVLTree;
@@ -1,9 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BTree = void 0;
4
- var BTree = /** @class */ (function () {
5
- function BTree() {
6
- }
7
- return BTree;
8
- }());
4
+ class BTree {
5
+ }
9
6
  exports.BTree = BTree;
@@ -1,8 +1,46 @@
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
+ */
1
8
  export declare class BinaryIndexedTree {
2
- private readonly _sumTree;
9
+ /**
10
+ * The constructor initializes an array with a specified length and fills it with zeros.
11
+ * @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
12
+ * sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
13
+ * The size of the sum tree array is `n + 1` because
14
+ */
3
15
  constructor(n: number);
16
+ private _sumTree;
17
+ get sumTree(): number[];
4
18
  static lowBit(x: number): number;
19
+ /**
20
+ * The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
21
+ * its ancestors.
22
+ * @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
23
+ * updated.
24
+ * @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
25
+ * at index "i" in the "_sumTree" array.
26
+ */
5
27
  update(i: number, delta: number): void;
28
+ /**
29
+ * The function calculates the prefix sum of an array using a binary indexed tree.
30
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
31
+ * array for which we want to calculate the prefix sum.
32
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
33
+ * `i`.
34
+ */
6
35
  getPrefixSum(i: number): number;
36
+ /**
37
+ * The function `getRangeSum` calculates the sum of a range of numbers in an array.
38
+ * @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
39
+ * sum.
40
+ * @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
41
+ * the sum.
42
+ * @returns the sum of the elements in the range specified by the start and end indices.
43
+ */
7
44
  getRangeSum(start: number, end: number): number;
45
+ protected _setSumTree(value: number[]): void;
8
46
  }
@@ -1,32 +1,73 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BinaryIndexedTree = void 0;
4
- var BinaryIndexedTree = /** @class */ (function () {
5
- function BinaryIndexedTree(n) {
4
+ /**
5
+ * data-structure-typed
6
+ *
7
+ * @author Tyler Zeng
8
+ * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
9
+ * @license MIT License
10
+ */
11
+ class BinaryIndexedTree {
12
+ /**
13
+ * The constructor initializes an array with a specified length and fills it with zeros.
14
+ * @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
15
+ * sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
16
+ * The size of the sum tree array is `n + 1` because
17
+ */
18
+ constructor(n) {
6
19
  this._sumTree = new Array(n + 1).fill(0);
7
20
  }
8
- BinaryIndexedTree.lowBit = function (x) {
21
+ get sumTree() {
22
+ return this._sumTree;
23
+ }
24
+ static lowBit(x) {
9
25
  return x & (-x);
10
- };
11
- BinaryIndexedTree.prototype.update = function (i, delta) {
26
+ }
27
+ /**
28
+ * The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
29
+ * its ancestors.
30
+ * @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
31
+ * updated.
32
+ * @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
33
+ * at index "i" in the "_sumTree" array.
34
+ */
35
+ update(i, delta) {
12
36
  while (i < this._sumTree.length) {
13
37
  this._sumTree[i] += delta;
14
38
  i += BinaryIndexedTree.lowBit(i);
15
39
  }
16
- };
17
- BinaryIndexedTree.prototype.getPrefixSum = function (i) {
18
- var sum = 0;
40
+ }
41
+ /**
42
+ * The function calculates the prefix sum of an array using a binary indexed tree.
43
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
44
+ * array for which we want to calculate the prefix sum.
45
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
46
+ * `i`.
47
+ */
48
+ getPrefixSum(i) {
49
+ let sum = 0;
19
50
  while (i > 0) {
20
51
  sum += this._sumTree[i];
21
52
  i -= BinaryIndexedTree.lowBit(i);
22
53
  }
23
54
  return sum;
24
- };
25
- BinaryIndexedTree.prototype.getRangeSum = function (start, end) {
55
+ }
56
+ /**
57
+ * The function `getRangeSum` calculates the sum of a range of numbers in an array.
58
+ * @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
59
+ * sum.
60
+ * @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
61
+ * the sum.
62
+ * @returns the sum of the elements in the range specified by the start and end indices.
63
+ */
64
+ getRangeSum(start, end) {
26
65
  if (!(0 <= start && start <= end && end <= this._sumTree.length))
27
66
  throw 'Index out of bounds';
28
67
  return this.getPrefixSum(end) - this.getPrefixSum(start);
29
- };
30
- return BinaryIndexedTree;
31
- }());
68
+ }
69
+ _setSumTree(value) {
70
+ this._sumTree = value;
71
+ }
72
+ }
32
73
  exports.BinaryIndexedTree = BinaryIndexedTree;