data-structure-typed 1.19.3 → 1.19.4

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 (155) hide show
  1. package/dist/data-structures/binary-tree/aa-tree.js +2 -5
  2. package/dist/data-structures/binary-tree/abstract-binary-tree.js +361 -488
  3. package/dist/data-structures/binary-tree/avl-tree.js +46 -90
  4. package/dist/data-structures/binary-tree/b-tree.js +2 -5
  5. package/dist/data-structures/binary-tree/binary-indexed-tree.js +17 -22
  6. package/dist/data-structures/binary-tree/binary-tree.js +9 -31
  7. package/dist/data-structures/binary-tree/bst.js +96 -139
  8. package/dist/data-structures/binary-tree/rb-tree.js +32 -56
  9. package/dist/data-structures/binary-tree/segment-tree.js +78 -120
  10. package/dist/data-structures/binary-tree/splay-tree.js +2 -5
  11. package/dist/data-structures/binary-tree/tree-multiset.js +176 -253
  12. package/dist/data-structures/binary-tree/two-three-tree.js +2 -5
  13. package/dist/data-structures/graph/abstract-graph.js +340 -574
  14. package/dist/data-structures/graph/directed-graph.js +146 -276
  15. package/dist/data-structures/graph/undirected-graph.js +87 -176
  16. package/dist/data-structures/hash/coordinate-map.js +23 -45
  17. package/dist/data-structures/hash/coordinate-set.js +20 -42
  18. package/dist/data-structures/hash/hash-table.js +2 -5
  19. package/dist/data-structures/hash/pair.js +2 -5
  20. package/dist/data-structures/hash/tree-map.js +2 -5
  21. package/dist/data-structures/hash/tree-set.js +2 -5
  22. package/dist/data-structures/heap/heap.js +53 -77
  23. package/dist/data-structures/heap/max-heap.js +8 -26
  24. package/dist/data-structures/heap/min-heap.js +8 -26
  25. package/dist/data-structures/linked-list/doubly-linked-list.js +132 -197
  26. package/dist/data-structures/linked-list/singly-linked-list.js +112 -173
  27. package/dist/data-structures/linked-list/skip-linked-list.js +2 -5
  28. package/dist/data-structures/matrix/matrix.js +7 -8
  29. package/dist/data-structures/matrix/matrix2d.js +76 -93
  30. package/dist/data-structures/matrix/navigator.js +18 -37
  31. package/dist/data-structures/matrix/vector2d.js +80 -101
  32. package/dist/data-structures/priority-queue/max-priority-queue.js +11 -39
  33. package/dist/data-structures/priority-queue/min-priority-queue.js +11 -39
  34. package/dist/data-structures/priority-queue/priority-queue.js +93 -139
  35. package/dist/data-structures/queue/deque.js +82 -128
  36. package/dist/data-structures/queue/queue.js +24 -25
  37. package/dist/data-structures/stack/stack.js +21 -22
  38. package/dist/data-structures/tree/tree.js +32 -45
  39. package/dist/data-structures/trie/trie.js +93 -200
  40. package/dist/utils/utils.js +22 -107
  41. package/dist/utils/validate-type.js +2 -2
  42. package/package.json +1 -1
  43. package/src/assets/complexities-diff.jpg +0 -0
  44. package/src/assets/data-structure-complexities.jpg +0 -0
  45. package/src/assets/logo.png +0 -0
  46. package/src/assets/overview-diagram-of-data-structures.png +0 -0
  47. package/src/data-structures/binary-tree/aa-tree.ts +3 -0
  48. package/src/data-structures/binary-tree/abstract-binary-tree.ts +1528 -0
  49. package/src/data-structures/binary-tree/avl-tree.ts +297 -0
  50. package/src/data-structures/binary-tree/b-tree.ts +3 -0
  51. package/src/data-structures/binary-tree/binary-indexed-tree.ts +78 -0
  52. package/src/data-structures/binary-tree/binary-tree.ts +40 -0
  53. package/src/data-structures/binary-tree/bst.ts +435 -0
  54. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  55. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  56. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  57. package/src/data-structures/binary-tree/index.ts +12 -0
  58. package/src/data-structures/binary-tree/rb-tree.ts +102 -0
  59. package/src/data-structures/binary-tree/segment-tree.ts +243 -0
  60. package/src/data-structures/binary-tree/splay-tree.ts +3 -0
  61. package/src/data-structures/binary-tree/tree-multiset.ts +694 -0
  62. package/src/data-structures/binary-tree/two-three-tree.ts +3 -0
  63. package/src/data-structures/diagrams/README.md +5 -0
  64. package/src/data-structures/graph/abstract-graph.ts +1032 -0
  65. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  66. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  67. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  68. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  69. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  70. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  71. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  72. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  73. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  74. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  75. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  76. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  77. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  78. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  79. package/src/data-structures/graph/directed-graph.ts +472 -0
  80. package/src/data-structures/graph/index.ts +3 -0
  81. package/src/data-structures/graph/undirected-graph.ts +270 -0
  82. package/src/data-structures/hash/coordinate-map.ts +67 -0
  83. package/src/data-structures/hash/coordinate-set.ts +56 -0
  84. package/src/data-structures/hash/hash-table.ts +3 -0
  85. package/src/data-structures/hash/index.ts +6 -0
  86. package/src/data-structures/hash/pair.ts +3 -0
  87. package/src/data-structures/hash/tree-map.ts +3 -0
  88. package/src/data-structures/hash/tree-set.ts +3 -0
  89. package/src/data-structures/heap/heap.ts +183 -0
  90. package/src/data-structures/heap/index.ts +3 -0
  91. package/src/data-structures/heap/max-heap.ts +31 -0
  92. package/src/data-structures/heap/min-heap.ts +34 -0
  93. package/src/data-structures/index.ts +15 -0
  94. package/src/data-structures/interfaces/abstract-binary-tree.ts +231 -0
  95. package/src/data-structures/interfaces/abstract-graph.ts +40 -0
  96. package/src/data-structures/interfaces/avl-tree.ts +28 -0
  97. package/src/data-structures/interfaces/binary-tree.ts +8 -0
  98. package/src/data-structures/interfaces/bst.ts +32 -0
  99. package/src/data-structures/interfaces/directed-graph.ts +20 -0
  100. package/src/data-structures/interfaces/doubly-linked-list.ts +1 -0
  101. package/src/data-structures/interfaces/heap.ts +1 -0
  102. package/src/data-structures/interfaces/index.ts +15 -0
  103. package/src/data-structures/interfaces/navigator.ts +1 -0
  104. package/src/data-structures/interfaces/priority-queue.ts +1 -0
  105. package/src/data-structures/interfaces/rb-tree.ts +11 -0
  106. package/src/data-structures/interfaces/segment-tree.ts +1 -0
  107. package/src/data-structures/interfaces/singly-linked-list.ts +1 -0
  108. package/src/data-structures/interfaces/tree-multiset.ts +12 -0
  109. package/src/data-structures/interfaces/undirected-graph.ts +6 -0
  110. package/src/data-structures/linked-list/doubly-linked-list.ts +573 -0
  111. package/src/data-structures/linked-list/index.ts +3 -0
  112. package/src/data-structures/linked-list/singly-linked-list.ts +490 -0
  113. package/src/data-structures/linked-list/skip-linked-list.ts +3 -0
  114. package/src/data-structures/matrix/index.ts +4 -0
  115. package/src/data-structures/matrix/matrix.ts +27 -0
  116. package/src/data-structures/matrix/matrix2d.ts +208 -0
  117. package/src/data-structures/matrix/navigator.ts +122 -0
  118. package/src/data-structures/matrix/vector2d.ts +316 -0
  119. package/src/data-structures/priority-queue/index.ts +3 -0
  120. package/src/data-structures/priority-queue/max-priority-queue.ts +49 -0
  121. package/src/data-structures/priority-queue/min-priority-queue.ts +50 -0
  122. package/src/data-structures/priority-queue/priority-queue.ts +354 -0
  123. package/src/data-structures/queue/deque.ts +251 -0
  124. package/src/data-structures/queue/index.ts +2 -0
  125. package/src/data-structures/queue/queue.ts +120 -0
  126. package/src/data-structures/stack/index.ts +1 -0
  127. package/src/data-structures/stack/stack.ts +98 -0
  128. package/src/data-structures/tree/index.ts +1 -0
  129. package/src/data-structures/tree/tree.ts +69 -0
  130. package/src/data-structures/trie/index.ts +1 -0
  131. package/src/data-structures/trie/trie.ts +227 -0
  132. package/src/data-structures/types/abstract-binary-tree.ts +42 -0
  133. package/src/data-structures/types/abstract-graph.ts +5 -0
  134. package/src/data-structures/types/avl-tree.ts +5 -0
  135. package/src/data-structures/types/binary-tree.ts +9 -0
  136. package/src/data-structures/types/bst.ts +12 -0
  137. package/src/data-structures/types/directed-graph.ts +8 -0
  138. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  139. package/src/data-structures/types/heap.ts +5 -0
  140. package/src/data-structures/types/helpers.ts +1 -0
  141. package/src/data-structures/types/index.ts +15 -0
  142. package/src/data-structures/types/navigator.ts +13 -0
  143. package/src/data-structures/types/priority-queue.ts +9 -0
  144. package/src/data-structures/types/rb-tree.ts +8 -0
  145. package/src/data-structures/types/segment-tree.ts +1 -0
  146. package/src/data-structures/types/singly-linked-list.ts +1 -0
  147. package/src/data-structures/types/tree-multiset.ts +8 -0
  148. package/src/index.ts +2 -0
  149. package/src/utils/index.ts +3 -0
  150. package/src/utils/types/index.ts +2 -0
  151. package/src/utils/types/utils.ts +6 -0
  152. package/src/utils/types/validate-type.ts +25 -0
  153. package/src/utils/utils.ts +78 -0
  154. package/src/utils/validate-type.ts +69 -0
  155. package/tsconfig.json +1 -1
@@ -0,0 +1,297 @@
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 {BST, BSTNode} from './bst';
9
+ import type {AVLTreeNodeNested, AVLTreeOptions, BinaryTreeDeletedResult, BinaryTreeNodeId} from '../types';
10
+ import {IAVLTree, IAVLTreeNode} from '../interfaces';
11
+
12
+ export class AVLTreeNode<T = any, NEIGHBOR extends AVLTreeNode<T, NEIGHBOR> = AVLTreeNodeNested<T>> extends BSTNode<T, NEIGHBOR> implements IAVLTreeNode<T, NEIGHBOR> {
13
+ }
14
+
15
+ export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
16
+ /**
17
+ * This is a constructor function for an AVL tree data structure in TypeScript.
18
+ * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
19
+ * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
20
+ * options.
21
+ */
22
+ constructor(options?: AVLTreeOptions) {
23
+ super(options);
24
+ }
25
+
26
+ /**
27
+ * The function creates a new AVL tree node with the given id and value.
28
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
29
+ * identify each node in the tree.
30
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
31
+ * that will be stored in the node.
32
+ * @returns a new AVLTreeNode object with the specified id and value.
33
+ */
34
+ override createNode(id: BinaryTreeNodeId, val?: N['val']): N {
35
+ return new AVLTreeNode<N['val'], N>(id, val) as N;
36
+ }
37
+
38
+ /**
39
+ * The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
40
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
41
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
42
+ * `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
43
+ * @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
44
+ */
45
+ override add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined {
46
+ const inserted = super.add(id, val);
47
+ if (inserted) this.balancePath(inserted);
48
+ return inserted;
49
+ }
50
+
51
+ /**
52
+ * The function overrides the remove method of the Binary Search Tree class, performs the removal operation, and
53
+ * then balances the tree if necessary.
54
+ * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
55
+ * removed from the AVL tree.
56
+ * @param {boolean} [isUpdateAllLeftSum] - The `isUpdateAllLeftSum` parameter is an optional boolean parameter that
57
+ * determines whether the left sum of all nodes in the AVL tree should be updated after removing a node. If
58
+ * `isUpdateAllLeftSum` is set to `true`, the left sum of all nodes will be recalculated.
59
+ * @returns The method is returning an array of `AVLTreeDeleted<N>` objects.
60
+ */
61
+ override remove(id: BinaryTreeNodeId, isUpdateAllLeftSum?: boolean): BinaryTreeDeletedResult<N>[] {
62
+ const deletedResults = super.remove(id, isUpdateAllLeftSum);
63
+ for (const {needBalanced} of deletedResults) {
64
+ if (needBalanced) {
65
+ this.balancePath(needBalanced);
66
+ }
67
+ }
68
+ return deletedResults;
69
+ }
70
+
71
+ /**
72
+ * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
73
+ * height of its right subtree.
74
+ * @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
75
+ * @returns The balance factor of the given AVL tree node.
76
+ */
77
+ balanceFactor(node: N): number {
78
+ if (!node.right) // node has no right subtree
79
+ return -node.height;
80
+ else if (!node.left) // node has no left subtree
81
+ return +node.height;
82
+ else
83
+ return node.right.height - node.left.height;
84
+ }
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: N): void {
91
+ if (!node.left && !node.right) // node is a leaf
92
+ node.height = 0;
93
+ else if (!node.left) {
94
+ // node has no left subtree
95
+ const rightHeight = node.right ? node.right.height : 0;
96
+ node.height = 1 + rightHeight;
97
+ } else if (!node.right) // node has no right subtree
98
+ node.height = 1 + node.left.height;
99
+ else
100
+ node.height = 1 + Math.max(node.right.height, node.left.height);
101
+ }
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: N): void {
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)) {
114
+ case -2:
115
+ if (A && A.left) {
116
+ if (this.balanceFactor(A.left) <= 0) {
117
+ this.balanceLL(A); // Perform LL rotation
118
+ } else {
119
+ this.balanceLR(A); // Perform LR rotation
120
+ }
121
+ }
122
+ break;
123
+ case +2:
124
+ if (A && A.right) {
125
+ if (this.balanceFactor(A.right) >= 0) {
126
+ this.balanceRR(A); // Perform RR rotation
127
+ } else {
128
+ this.balanceRL(A); // Perform RL rotation
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ /**
136
+ * The `balanceLL` function performs a left-left rotation on an AVL tree to balance it.
137
+ * @param A - The parameter A is an AVLTreeNode object.
138
+ */
139
+ balanceLL(A: N): void {
140
+ const parentOfA = A.parent;
141
+ const B = A.left; // A is left-heavy and B is left-heavy
142
+ A.parent = B;
143
+ if (B && B.right) {
144
+ B.right.parent = A;
145
+ }
146
+ if (B) B.parent = parentOfA;
147
+ if (A === this.root) {
148
+ if (B) this._setRoot(B);
149
+ } else {
150
+ if (parentOfA?.left === A) {
151
+ parentOfA.left = B;
152
+ } else {
153
+ if (parentOfA) parentOfA.right = B;
154
+ }
155
+ }
156
+
157
+ if (B) {
158
+ A.left = B.right; // Make T2 the left subtree of A
159
+ B.right = A; // Make A the left child of B
160
+ }
161
+ this.updateHeight(A);
162
+ if (B) this.updateHeight(B);
163
+ }
164
+
165
+ /**
166
+ * The `balanceLR` function performs a left-right rotation to balance an AVL tree.
167
+ * @param A - A is an AVLTreeNode object.
168
+ */
169
+ balanceLR(A: N): void {
170
+ const parentOfA = A.parent;
171
+ const B = A.left; // A is left-heavy
172
+ let C = null;
173
+ if (B) {
174
+ C = B.right;// B is right-heavy
175
+ }
176
+ if (A) A.parent = C;
177
+ if (B) B.parent = C;
178
+
179
+ if (C) {
180
+ if (C.left) {
181
+ C.left.parent = B;
182
+ }
183
+ if (C.right) {
184
+ C.right.parent = A;
185
+ }
186
+ C.parent = parentOfA;
187
+ }
188
+
189
+ if (A === this.root) {
190
+ if (C) this._setRoot(C);
191
+ } else {
192
+ if (parentOfA) {
193
+ if (parentOfA.left === A) {
194
+ parentOfA.left = C;
195
+ } else {
196
+ parentOfA.right = C;
197
+ }
198
+ }
199
+ }
200
+
201
+ if (C) {
202
+ A.left = C.right; // Make T3 the left subtree of A
203
+ if (B) B.right = C.left; // Make T2 the right subtree of B
204
+ C.left = B;
205
+ C.right = A;
206
+ }
207
+
208
+ this.updateHeight(A); // Adjust heights
209
+ B && this.updateHeight(B);
210
+ C && this.updateHeight(C);
211
+ }
212
+
213
+ /**
214
+ * The `balanceRR` function performs a right-right rotation on an AVL tree to balance it.
215
+ * @param A - The parameter A is an AVLTreeNode object.
216
+ */
217
+ balanceRR(A: N): void {
218
+ const parentOfA = A.parent;
219
+ const B = A.right; // A is right-heavy and B is right-heavy
220
+ A.parent = B;
221
+ if (B) {
222
+ if (B.left) {
223
+ B.left.parent = A;
224
+ }
225
+ B.parent = parentOfA;
226
+ }
227
+
228
+ if (A === this.root) {
229
+ if (B) this._setRoot(B);
230
+ } else {
231
+ if (parentOfA) {
232
+ if (parentOfA.left === A) {
233
+ parentOfA.left = B;
234
+ } else {
235
+ parentOfA.right = B;
236
+ }
237
+ }
238
+ }
239
+
240
+ if (B) {
241
+ A.right = B.left; // Make T2 the right subtree of A
242
+ B.left = A;
243
+ }
244
+ this.updateHeight(A);
245
+ B && this.updateHeight(B);
246
+ }
247
+
248
+ /**
249
+ * The `balanceRL` function performs a right-left rotation to balance an AVL tree.
250
+ * @param A - A is an AVLTreeNode object.
251
+ */
252
+ balanceRL(A: N): void {
253
+ const parentOfA = A.parent;
254
+ const B = A.right; // A is right-heavy
255
+ let C = null;
256
+ if (B) {
257
+ C = B.left; // B is left-heavy
258
+ }
259
+
260
+ A.parent = C;
261
+ if (B) B.parent = C;
262
+
263
+ if (C) {
264
+ if (C.left) {
265
+ C.left.parent = A;
266
+ }
267
+ if (C.right) {
268
+ C.right.parent = B;
269
+ }
270
+ C.parent = parentOfA;
271
+ }
272
+
273
+
274
+ if (A === this.root) {
275
+ if (C) this._setRoot(C);
276
+ } else {
277
+ if (parentOfA) {
278
+ if (parentOfA.left === A) {
279
+ parentOfA.left = C;
280
+ } else {
281
+ parentOfA.right = C;
282
+ }
283
+ }
284
+ }
285
+
286
+ if (C) A.right = C.left; // Make T2 the right subtree of A
287
+ if (B && C) B.left = C.right; // Make T3 the left subtree of B
288
+ if (C) C.left = A;
289
+ if (C) C.right = B;
290
+
291
+ this.updateHeight(A); // Adjust heights
292
+ B && this.updateHeight(B);
293
+ C && this.updateHeight(C);
294
+ }
295
+ }
296
+
297
+
@@ -0,0 +1,3 @@
1
+ export class BTree {
2
+
3
+ }
@@ -0,0 +1,78 @@
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 BinaryIndexedTree {
9
+
10
+ /**
11
+ * The constructor initializes an array with a specified length and fills it with zeros.
12
+ * @param {number} n - The parameter `n` represents the size of the array that will be used to store the sum tree. The
13
+ * sum tree is a binary tree data structure used to efficiently calculate the sum of a range of elements in an array.
14
+ * The size of the sum tree array is `n + 1` because
15
+ */
16
+ constructor(n: number) {
17
+ this._sumTree = new Array<number>(n + 1).fill(0);
18
+ }
19
+
20
+ private _sumTree: number[];
21
+
22
+ get sumTree(): number[] {
23
+ return this._sumTree;
24
+ }
25
+
26
+ static lowBit(x: number) {
27
+ return x & (-x);
28
+ }
29
+
30
+ /**
31
+ * The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
32
+ * its ancestors.
33
+ * @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
34
+ * updated.
35
+ * @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
36
+ * at index "i" in the "_sumTree" array.
37
+ */
38
+ update(i: number, delta: number) {
39
+ while (i < this._sumTree.length) {
40
+ this._sumTree[i] += delta;
41
+ i += BinaryIndexedTree.lowBit(i);
42
+ }
43
+ }
44
+
45
+ /**
46
+ * The function calculates the prefix sum of an array using a binary indexed tree.
47
+ * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
48
+ * array for which we want to calculate the prefix sum.
49
+ * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
50
+ * `i`.
51
+ */
52
+ getPrefixSum(i: number) {
53
+ let sum = 0;
54
+ while (i > 0) {
55
+ sum += this._sumTree[i];
56
+ i -= BinaryIndexedTree.lowBit(i);
57
+ }
58
+ return sum;
59
+ }
60
+
61
+ /**
62
+ * The function `getRangeSum` calculates the sum of a range of numbers in an array.
63
+ * @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
64
+ * sum.
65
+ * @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
66
+ * the sum.
67
+ * @returns the sum of the elements in the range specified by the start and end indices.
68
+ */
69
+ getRangeSum(start: number, end: number): number {
70
+ if (!(0 <= start && start <= end && end <= this._sumTree.length))
71
+ throw 'Index out of bounds';
72
+ return this.getPrefixSum(end) - this.getPrefixSum(start);
73
+ }
74
+
75
+ protected _setSumTree(value: number[]) {
76
+ this._sumTree = value;
77
+ }
78
+ }
@@ -0,0 +1,40 @@
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 type {BinaryTreeNodeId, BinaryTreeNodeNested, BinaryTreeOptions} from '../types';
10
+ import {AbstractBinaryTree, AbstractBinaryTreeNode} from './abstract-binary-tree';
11
+ import {IBinaryTree, IBinaryTreeNode} from '../interfaces/binary-tree';
12
+
13
+ export class BinaryTreeNode<T = any, NEIGHBOR extends BinaryTreeNode<T, NEIGHBOR> = BinaryTreeNodeNested<T>> extends AbstractBinaryTreeNode<T, NEIGHBOR> implements IBinaryTreeNode<T, NEIGHBOR> {
14
+
15
+ }
16
+
17
+ export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode> extends AbstractBinaryTree<N> implements IBinaryTree<N> {
18
+
19
+ /**
20
+ * This is a constructor function for a binary tree class that takes an optional options parameter.
21
+ * @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
22
+ * constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
23
+ * different configuration options.
24
+ */
25
+ constructor(options?: BinaryTreeOptions) {
26
+ super(options);
27
+ }
28
+
29
+ /**
30
+ * The function creates a new binary tree node with an optional value.
31
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
32
+ * `BinaryTreeNodeId`, which represents the unique identifier for each node in the binary tree.
33
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
34
+ * stored in the node.
35
+ * @returns a new instance of a BinaryTreeNode with the specified id and value.
36
+ */
37
+ createNode(id: BinaryTreeNodeId, val?: N['val']): N {
38
+ return new BinaryTreeNode<N['val'], N>(id, val) as N;
39
+ }
40
+ }