data-structure-typed 1.33.7 → 1.33.9

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 (149) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +25 -14
  3. package/package.json +22 -22
  4. package/.prettierrc.js +0 -16
  5. package/coverage/coverage-final.json +0 -68
  6. package/coverage/coverage-summary.json +0 -69
  7. package/docs/.nojekyll +0 -1
  8. package/docs/index.html +0 -875
  9. package/docs/modules.html +0 -336
  10. package/jest.config.js +0 -8
  11. package/src/data-structures/binary-tree/aa-tree.ts +0 -1
  12. package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1608
  13. package/src/data-structures/binary-tree/avl-tree.ts +0 -307
  14. package/src/data-structures/binary-tree/b-tree.ts +0 -1
  15. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -76
  16. package/src/data-structures/binary-tree/binary-tree.ts +0 -47
  17. package/src/data-structures/binary-tree/bst.ts +0 -537
  18. package/src/data-structures/binary-tree/index.ts +0 -12
  19. package/src/data-structures/binary-tree/rb-tree.ts +0 -366
  20. package/src/data-structures/binary-tree/segment-tree.ts +0 -260
  21. package/src/data-structures/binary-tree/splay-tree.ts +0 -1
  22. package/src/data-structures/binary-tree/tree-multiset.ts +0 -700
  23. package/src/data-structures/binary-tree/two-three-tree.ts +0 -1
  24. package/src/data-structures/graph/abstract-graph.ts +0 -1040
  25. package/src/data-structures/graph/directed-graph.ts +0 -470
  26. package/src/data-structures/graph/index.ts +0 -4
  27. package/src/data-structures/graph/map-graph.ts +0 -129
  28. package/src/data-structures/graph/undirected-graph.ts +0 -274
  29. package/src/data-structures/hash/coordinate-map.ts +0 -67
  30. package/src/data-structures/hash/coordinate-set.ts +0 -56
  31. package/src/data-structures/hash/hash-map.ts +0 -203
  32. package/src/data-structures/hash/hash-table.ts +0 -277
  33. package/src/data-structures/hash/index.ts +0 -7
  34. package/src/data-structures/hash/pair.ts +0 -1
  35. package/src/data-structures/hash/tree-map.ts +0 -1
  36. package/src/data-structures/hash/tree-set.ts +0 -1
  37. package/src/data-structures/heap/heap.ts +0 -212
  38. package/src/data-structures/heap/index.ts +0 -3
  39. package/src/data-structures/heap/max-heap.ts +0 -31
  40. package/src/data-structures/heap/min-heap.ts +0 -32
  41. package/src/data-structures/index.ts +0 -11
  42. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -636
  43. package/src/data-structures/linked-list/index.ts +0 -3
  44. package/src/data-structures/linked-list/singly-linked-list.ts +0 -501
  45. package/src/data-structures/linked-list/skip-linked-list.ts +0 -166
  46. package/src/data-structures/matrix/index.ts +0 -4
  47. package/src/data-structures/matrix/matrix.ts +0 -27
  48. package/src/data-structures/matrix/matrix2d.ts +0 -213
  49. package/src/data-structures/matrix/navigator.ts +0 -121
  50. package/src/data-structures/matrix/vector2d.ts +0 -316
  51. package/src/data-structures/priority-queue/index.ts +0 -3
  52. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -56
  53. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -57
  54. package/src/data-structures/priority-queue/priority-queue.ts +0 -359
  55. package/src/data-structures/queue/deque.ts +0 -297
  56. package/src/data-structures/queue/index.ts +0 -2
  57. package/src/data-structures/queue/queue.ts +0 -191
  58. package/src/data-structures/stack/index.ts +0 -1
  59. package/src/data-structures/stack/stack.ts +0 -98
  60. package/src/data-structures/tree/index.ts +0 -1
  61. package/src/data-structures/tree/tree.ts +0 -69
  62. package/src/data-structures/trie/index.ts +0 -1
  63. package/src/data-structures/trie/trie.ts +0 -225
  64. package/src/index.ts +0 -4
  65. package/src/interfaces/abstract-binary-tree.ts +0 -189
  66. package/src/interfaces/abstract-graph.ts +0 -31
  67. package/src/interfaces/avl-tree.ts +0 -25
  68. package/src/interfaces/binary-tree.ts +0 -6
  69. package/src/interfaces/bst.ts +0 -31
  70. package/src/interfaces/directed-graph.ts +0 -20
  71. package/src/interfaces/doubly-linked-list.ts +0 -1
  72. package/src/interfaces/heap.ts +0 -1
  73. package/src/interfaces/index.ts +0 -15
  74. package/src/interfaces/navigator.ts +0 -1
  75. package/src/interfaces/priority-queue.ts +0 -1
  76. package/src/interfaces/rb-tree.ts +0 -9
  77. package/src/interfaces/segment-tree.ts +0 -1
  78. package/src/interfaces/singly-linked-list.ts +0 -1
  79. package/src/interfaces/tree-multiset.ts +0 -7
  80. package/src/interfaces/undirected-graph.ts +0 -6
  81. package/src/types/data-structures/abstract-binary-tree.ts +0 -50
  82. package/src/types/data-structures/abstract-graph.ts +0 -11
  83. package/src/types/data-structures/avl-tree.ts +0 -5
  84. package/src/types/data-structures/binary-tree.ts +0 -5
  85. package/src/types/data-structures/bst.ts +0 -13
  86. package/src/types/data-structures/directed-graph.ts +0 -8
  87. package/src/types/data-structures/doubly-linked-list.ts +0 -1
  88. package/src/types/data-structures/hash.ts +0 -1
  89. package/src/types/data-structures/heap.ts +0 -5
  90. package/src/types/data-structures/index.ts +0 -16
  91. package/src/types/data-structures/map-graph.ts +0 -1
  92. package/src/types/data-structures/navigator.ts +0 -13
  93. package/src/types/data-structures/priority-queue.ts +0 -9
  94. package/src/types/data-structures/rb-tree.ts +0 -8
  95. package/src/types/data-structures/segment-tree.ts +0 -1
  96. package/src/types/data-structures/singly-linked-list.ts +0 -1
  97. package/src/types/data-structures/tree-multiset.ts +0 -6
  98. package/src/types/helpers.ts +0 -1
  99. package/src/types/index.ts +0 -3
  100. package/src/types/utils/index.ts +0 -2
  101. package/src/types/utils/utils.ts +0 -6
  102. package/src/types/utils/validate-type.ts +0 -35
  103. package/src/utils/index.ts +0 -1
  104. package/src/utils/utils.ts +0 -79
  105. package/test/integration/avl-tree.test.ts +0 -108
  106. package/test/integration/bst.test.ts +0 -380
  107. package/test/integration/heap.test.js +0 -16
  108. package/test/integration/index.html +0 -44
  109. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +0 -108
  110. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -142
  111. package/test/unit/data-structures/binary-tree/bst.test.ts +0 -380
  112. package/test/unit/data-structures/binary-tree/overall.test.ts +0 -65
  113. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +0 -43
  114. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +0 -50
  115. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +0 -461
  116. package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -5
  117. package/test/unit/data-structures/graph/directed-graph.test.ts +0 -519
  118. package/test/unit/data-structures/graph/index.ts +0 -2
  119. package/test/unit/data-structures/graph/map-graph.test.ts +0 -45
  120. package/test/unit/data-structures/graph/overall.test.ts +0 -49
  121. package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -59
  122. package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -54
  123. package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -41
  124. package/test/unit/data-structures/hash/hash-map.test.ts +0 -104
  125. package/test/unit/data-structures/hash/hash-table.test.ts +0 -184
  126. package/test/unit/data-structures/heap/heap.test.ts +0 -55
  127. package/test/unit/data-structures/heap/max-heap.test.ts +0 -44
  128. package/test/unit/data-structures/heap/min-heap.test.ts +0 -82
  129. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -364
  130. package/test/unit/data-structures/linked-list/index.ts +0 -4
  131. package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -35
  132. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -451
  133. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +0 -13
  134. package/test/unit/data-structures/linked-list/skip-list.test.ts +0 -55
  135. package/test/unit/data-structures/matrix/matrix.test.ts +0 -54
  136. package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -138
  137. package/test/unit/data-structures/matrix/navigator.test.ts +0 -79
  138. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -106
  139. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +0 -105
  140. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -27
  141. package/test/unit/data-structures/queue/deque.test.ts +0 -130
  142. package/test/unit/data-structures/queue/queue.test.ts +0 -199
  143. package/test/unit/data-structures/stack/stack.test.ts +0 -67
  144. package/test/unit/data-structures/tree/tree.test.ts +0 -39
  145. package/test/unit/data-structures/trie/trie.test.ts +0 -95
  146. package/test/utils/index.ts +0 -2
  147. package/test/utils/magnitude.ts +0 -21
  148. package/test/utils/number.ts +0 -3
  149. package/tsconfig.json +0 -29
@@ -1,307 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- 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<V = any, NEIGHBOR extends AVLTreeNode<V, NEIGHBOR> = AVLTreeNodeNested<V>>
13
- extends BSTNode<V, NEIGHBOR>
14
- implements IAVLTreeNode<V, NEIGHBOR>
15
- {
16
- constructor(id: BinaryTreeNodeId, val?: V) {
17
- super(id, val);
18
- }
19
- }
20
-
21
- export class AVLTree<N extends AVLTreeNode<N['val'], N> = AVLTreeNode> extends BST<N> implements IAVLTree<N> {
22
- /**
23
- * This is a constructor function for an AVL tree data structure in TypeScript.
24
- * @param {AVLTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
25
- * constructor of the AVLTree class. It allows you to customize the behavior of the AVL tree by providing different
26
- * options.
27
- */
28
- constructor(options?: AVLTreeOptions) {
29
- super(options);
30
- }
31
-
32
- /**
33
- * The function creates a new AVL tree node with the given id and value.
34
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is used to uniquely
35
- * identify each node in the tree.
36
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
37
- * that will be stored in the node.
38
- * @returns a new AVLTreeNode object with the specified id and value.
39
- */
40
- override createNode(id: BinaryTreeNodeId, val?: N['val']): N {
41
- return new AVLTreeNode<N['val'], N>(id, val) as N;
42
- }
43
-
44
- /**
45
- * The function overrides the add method of a binary tree node and balances the tree after inserting a new node.
46
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that we want to add.
47
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. It is of type
48
- * `N['val']`, which means it should be of the same type as the `val` property of the nodes in the binary tree.
49
- * @returns The method is returning the inserted node, or null or undefined if the insertion was not successful.
50
- */
51
- override add(id: BinaryTreeNodeId, val?: N['val']): N | null | undefined {
52
- // TODO support node as a param
53
- const inserted = super.add(id, val);
54
- if (inserted) this._balancePath(inserted);
55
- return inserted;
56
- }
57
-
58
- /**
59
- * The function overrides the remove method of a binary tree and performs additional operations to balance the tree after
60
- * deletion.
61
- * @param {BinaryTreeNodeId} id - The `id` parameter represents the identifier of the binary tree node that needs to be
62
- * removed.
63
- * @returns The method is returning an array of `BinaryTreeDeletedResult<N>` objects.
64
- */
65
- override remove(id: BinaryTreeNodeId): BinaryTreeDeletedResult<N>[] {
66
- const deletedResults = super.remove(id);
67
- for (const {needBalanced} of deletedResults) {
68
- if (needBalanced) {
69
- this._balancePath(needBalanced);
70
- }
71
- }
72
- return deletedResults;
73
- }
74
-
75
- /**
76
- * The balance factor of a given AVL tree node is calculated by subtracting the height of its left subtree from the
77
- * height of its right subtree.
78
- * @param node - The parameter "node" is of type N, which represents a node in an AVL tree.
79
- * @returns The balance factor of the given AVL tree node.
80
- */
81
- protected _balanceFactor(node: N): number {
82
- if (!node.right)
83
- // node has no right subtree
84
- return -node.height;
85
- else if (!node.left)
86
- // node has no left subtree
87
- return +node.height;
88
- else return node.right.height - node.left.height;
89
- }
90
-
91
- /**
92
- * The function updates the height of a node in an AVL tree based on the heights of its left and right subtrees.
93
- * @param node - The parameter `node` is an AVLTreeNode object, which represents a node in an AVL tree.
94
- */
95
- protected _updateHeight(node: N): void {
96
- if (!node.left && !node.right) node.height = 0;
97
- else if (!node.left) {
98
- const rightHeight = node.right ? node.right.height : 0;
99
- node.height = 1 + rightHeight;
100
- } else if (!node.right) node.height = 1 + node.left.height;
101
- else node.height = 1 + Math.max(node.right.height, node.left.height);
102
- }
103
-
104
- /**
105
- * The `_balancePath` function balances the AVL tree by performing appropriate rotations based on the balance factor of
106
- * each node in the path from the given node to the root.
107
- * @param node - The `node` parameter is an AVLTreeNode object, which represents a node in an AVL tree.
108
- */
109
- protected _balancePath(node: N): void {
110
- const path = this.getPathToRoot(node, false); // first O(log n) + O(log n)
111
- for (let i = 0; i < path.length; i++) {
112
- // second O(log n)
113
- const A = path[i];
114
- // Update Heights: After inserting a node, backtrack from the insertion point to the root node, updating the height of each node along the way.
115
- this._updateHeight(A); // first O(1)
116
- // Check Balance: Simultaneously with height updates, check if each node violates the balance property of an AVL tree.
117
- // Balance Restoration: If a balance issue is discovered after inserting a node, it requires balance restoration operations. Balance restoration includes four basic cases where rotation operations need to be performed to fix the balance:
118
- switch (
119
- this._balanceFactor(A) // second O(1)
120
- ) {
121
- case -2:
122
- if (A && A.left) {
123
- if (this._balanceFactor(A.left) <= 0) {
124
- // second O(1)
125
- // Left Rotation (LL Rotation): When the inserted node is in the left subtree of the left subtree, causing an imbalance.
126
- this._balanceLL(A);
127
- } else {
128
- // Left-Right Rotation (LR Rotation): When the inserted node is in the right subtree of the left subtree, causing an imbalance.
129
- this._balanceLR(A);
130
- }
131
- }
132
- break;
133
- case +2:
134
- if (A && A.right) {
135
- if (this._balanceFactor(A.right) >= 0) {
136
- // Right Rotation (RR Rotation): When the inserted node is in the right subtree of the right subtree, causing an imbalance.
137
- this._balanceRR(A);
138
- } else {
139
- // Right-Left Rotation (RL Rotation): When the inserted node is in the left subtree of the right subtree, causing an imbalance.
140
- this._balanceRL(A);
141
- }
142
- }
143
- }
144
- // TODO So far, no sure if this is necessary that Recursive Repair: Once rotation operations are executed, it may cause imbalance issues at higher levels of the tree. Therefore, you need to recursively check and repair imbalance problems upwards until you reach the root node.
145
- }
146
- }
147
-
148
- /**
149
- * The `_balanceLL` function performs a left-left rotation on an AVL tree to balance it.
150
- * @param A - The parameter A is an AVLTreeNode object.
151
- */
152
- protected _balanceLL(A: N): void {
153
- const parentOfA = A.parent;
154
- const B = A.left;
155
- A.parent = B;
156
- if (B && B.right) {
157
- B.right.parent = A;
158
- }
159
- if (B) B.parent = parentOfA;
160
- if (A === this.root) {
161
- if (B) this._setRoot(B);
162
- } else {
163
- if (parentOfA?.left === A) {
164
- parentOfA.left = B;
165
- } else {
166
- if (parentOfA) parentOfA.right = B;
167
- }
168
- }
169
-
170
- if (B) {
171
- A.left = B.right;
172
- B.right = A;
173
- }
174
- this._updateHeight(A);
175
- if (B) this._updateHeight(B);
176
- }
177
-
178
- /**
179
- * The `_balanceLR` function performs a left-right rotation to balance an AVL tree.
180
- * @param A - A is an AVLTreeNode object.
181
- */
182
- protected _balanceLR(A: N): void {
183
- const parentOfA = A.parent;
184
- const B = A.left;
185
- let C = null;
186
- if (B) {
187
- C = B.right;
188
- }
189
- if (A) A.parent = C;
190
- if (B) B.parent = C;
191
-
192
- if (C) {
193
- if (C.left) {
194
- C.left.parent = B;
195
- }
196
- if (C.right) {
197
- C.right.parent = A;
198
- }
199
- C.parent = parentOfA;
200
- }
201
-
202
- if (A === this.root) {
203
- if (C) this._setRoot(C);
204
- } else {
205
- if (parentOfA) {
206
- if (parentOfA.left === A) {
207
- parentOfA.left = C;
208
- } else {
209
- parentOfA.right = C;
210
- }
211
- }
212
- }
213
-
214
- if (C) {
215
- A.left = C.right;
216
- if (B) B.right = C.left;
217
- C.left = B;
218
- C.right = A;
219
- }
220
-
221
- this._updateHeight(A);
222
- B && this._updateHeight(B);
223
- C && this._updateHeight(C);
224
- }
225
-
226
- /**
227
- * The `_balanceRR` function performs a right-right rotation on an AVL tree to balance it.
228
- * @param A - The parameter A is an AVLTreeNode object.
229
- */
230
- protected _balanceRR(A: N): void {
231
- const parentOfA = A.parent;
232
- const B = A.right;
233
- A.parent = B;
234
- if (B) {
235
- if (B.left) {
236
- B.left.parent = A;
237
- }
238
- B.parent = parentOfA;
239
- }
240
-
241
- if (A === this.root) {
242
- if (B) this._setRoot(B);
243
- } else {
244
- if (parentOfA) {
245
- if (parentOfA.left === A) {
246
- parentOfA.left = B;
247
- } else {
248
- parentOfA.right = B;
249
- }
250
- }
251
- }
252
-
253
- if (B) {
254
- A.right = B.left;
255
- B.left = A;
256
- }
257
- this._updateHeight(A);
258
- B && this._updateHeight(B);
259
- }
260
-
261
- /**
262
- * The `_balanceRL` function performs a right-left rotation to balance an AVL tree.
263
- * @param A - A is an AVLTreeNode object.
264
- */
265
- protected _balanceRL(A: N): void {
266
- const parentOfA = A.parent;
267
- const B = A.right;
268
- let C = null;
269
- if (B) {
270
- C = B.left;
271
- }
272
-
273
- A.parent = C;
274
- if (B) B.parent = C;
275
-
276
- if (C) {
277
- if (C.left) {
278
- C.left.parent = A;
279
- }
280
- if (C.right) {
281
- C.right.parent = B;
282
- }
283
- C.parent = parentOfA;
284
- }
285
-
286
- if (A === this.root) {
287
- if (C) this._setRoot(C);
288
- } else {
289
- if (parentOfA) {
290
- if (parentOfA.left === A) {
291
- parentOfA.left = C;
292
- } else {
293
- parentOfA.right = C;
294
- }
295
- }
296
- }
297
-
298
- if (C) A.right = C.left;
299
- if (B && C) B.left = C.right;
300
- if (C) C.left = A;
301
- if (C) C.right = B;
302
-
303
- this._updateHeight(A);
304
- B && this._updateHeight(B);
305
- C && this._updateHeight(C);
306
- }
307
- }
@@ -1 +0,0 @@
1
- export class BTree {}
@@ -1,76 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- export class BinaryIndexedTree {
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
- */
15
- constructor(n: number) {
16
- this._sumTree = new Array<number>(n + 1).fill(0);
17
- }
18
-
19
- private _sumTree: number[];
20
-
21
- get sumTree(): number[] {
22
- return this._sumTree;
23
- }
24
-
25
- static lowBit(x: number) {
26
- return x & -x;
27
- }
28
-
29
- /**
30
- * The update function updates the values in a binary indexed tree by adding a delta value to the specified index and
31
- * its ancestors.
32
- * @param {number} i - The parameter `i` represents the index of the element in the `_sumTree` array that needs to be
33
- * updated.
34
- * @param {number} delta - The "delta" parameter represents the change in value that needs to be added to the element
35
- * at index "i" in the "_sumTree" array.
36
- */
37
- update(i: number, delta: number) {
38
- while (i < this._sumTree.length) {
39
- this._sumTree[i] += delta;
40
- i += BinaryIndexedTree.lowBit(i);
41
- }
42
- }
43
-
44
- /**
45
- * The function calculates the prefix sum of an array using a binary indexed tree.
46
- * @param {number} i - The parameter "i" in the function "getPrefixSum" represents the index of the element in the
47
- * array for which we want to calculate the prefix sum.
48
- * @returns The function `getPrefixSum` returns the prefix sum of the elements in the binary indexed tree up to index
49
- * `i`.
50
- */
51
- getPrefixSum(i: number) {
52
- let sum = 0;
53
- while (i > 0) {
54
- sum += this._sumTree[i];
55
- i -= BinaryIndexedTree.lowBit(i);
56
- }
57
- return sum;
58
- }
59
-
60
- /**
61
- * The function `getRangeSum` calculates the sum of a range of numbers in an array.
62
- * @param {number} start - The start parameter is the starting index of the range for which we want to calculate the
63
- * sum.
64
- * @param {number} end - The "end" parameter represents the ending index of the range for which we want to calculate
65
- * the sum.
66
- * @returns the sum of the elements in the range specified by the start and end indices.
67
- */
68
- getRangeSum(start: number, end: number): number {
69
- if (!(0 <= start && start <= end && end <= this._sumTree.length)) throw 'Index out of bounds';
70
- return this.getPrefixSum(end) - this.getPrefixSum(start);
71
- }
72
-
73
- protected _setSumTree(value: number[]) {
74
- this._sumTree = value;
75
- }
76
- }
@@ -1,47 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Tyler Zeng
5
- * @copyright Copyright (c) 2022 Tyler Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
-
9
- import type {BinaryTreeNodeId, BinaryTreeNodeNested, BinaryTreeOptions} from '../../types';
10
- import {AbstractBinaryTree, AbstractBinaryTreeNode} from './abstract-binary-tree';
11
- import {IBinaryTree, IBinaryTreeNode} from '../../interfaces';
12
-
13
- export class BinaryTreeNode<V = any, NEIGHBOR extends BinaryTreeNode<V, NEIGHBOR> = BinaryTreeNodeNested<V>>
14
- extends AbstractBinaryTreeNode<V, NEIGHBOR>
15
- implements IBinaryTreeNode<V, NEIGHBOR>
16
- {
17
- constructor(id: BinaryTreeNodeId, val?: V) {
18
- super(id, val);
19
- }
20
- }
21
-
22
- export class BinaryTree<N extends BinaryTreeNode<N['val'], N> = BinaryTreeNode>
23
- extends AbstractBinaryTree<N>
24
- implements IBinaryTree<N>
25
- {
26
- /**
27
- * This is a constructor function for a binary tree class that takes an optional options parameter.
28
- * @param {BinaryTreeOptions} [options] - The `options` parameter is an optional object that can be passed to the
29
- * constructor of the `BinaryTree` class. It allows you to customize the behavior of the binary tree by providing
30
- * different configuration options.
31
- */
32
- constructor(options?: BinaryTreeOptions) {
33
- super(options);
34
- }
35
-
36
- /**
37
- * The function creates a new binary tree node with an optional value.
38
- * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
39
- * `BinaryTreeNodeId`, which represents the unique identifier for each node in the binary tree.
40
- * @param [val] - The `val` parameter is an optional value that can be assigned to the node. It represents the value
41
- * stored in the node.
42
- * @returns a new instance of a BinaryTreeNode with the specified id and value.
43
- */
44
- createNode(id: BinaryTreeNodeId, val?: N['val']): N {
45
- return new BinaryTreeNode<N['val'], N>(id, val) as N;
46
- }
47
- }