data-structure-typed 1.33.8 → 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 (148) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/package.json +1 -1
  3. package/.prettierrc.js +0 -16
  4. package/coverage/coverage-final.json +0 -68
  5. package/coverage/coverage-summary.json +0 -69
  6. package/docs/.nojekyll +0 -1
  7. package/docs/index.html +0 -877
  8. package/docs/modules.html +0 -336
  9. package/jest.config.js +0 -8
  10. package/src/data-structures/binary-tree/aa-tree.ts +0 -1
  11. package/src/data-structures/binary-tree/abstract-binary-tree.ts +0 -1608
  12. package/src/data-structures/binary-tree/avl-tree.ts +0 -307
  13. package/src/data-structures/binary-tree/b-tree.ts +0 -1
  14. package/src/data-structures/binary-tree/binary-indexed-tree.ts +0 -76
  15. package/src/data-structures/binary-tree/binary-tree.ts +0 -47
  16. package/src/data-structures/binary-tree/bst.ts +0 -537
  17. package/src/data-structures/binary-tree/index.ts +0 -12
  18. package/src/data-structures/binary-tree/rb-tree.ts +0 -366
  19. package/src/data-structures/binary-tree/segment-tree.ts +0 -260
  20. package/src/data-structures/binary-tree/splay-tree.ts +0 -1
  21. package/src/data-structures/binary-tree/tree-multiset.ts +0 -700
  22. package/src/data-structures/binary-tree/two-three-tree.ts +0 -1
  23. package/src/data-structures/graph/abstract-graph.ts +0 -1040
  24. package/src/data-structures/graph/directed-graph.ts +0 -470
  25. package/src/data-structures/graph/index.ts +0 -4
  26. package/src/data-structures/graph/map-graph.ts +0 -129
  27. package/src/data-structures/graph/undirected-graph.ts +0 -274
  28. package/src/data-structures/hash/coordinate-map.ts +0 -67
  29. package/src/data-structures/hash/coordinate-set.ts +0 -56
  30. package/src/data-structures/hash/hash-map.ts +0 -203
  31. package/src/data-structures/hash/hash-table.ts +0 -277
  32. package/src/data-structures/hash/index.ts +0 -7
  33. package/src/data-structures/hash/pair.ts +0 -1
  34. package/src/data-structures/hash/tree-map.ts +0 -1
  35. package/src/data-structures/hash/tree-set.ts +0 -1
  36. package/src/data-structures/heap/heap.ts +0 -212
  37. package/src/data-structures/heap/index.ts +0 -3
  38. package/src/data-structures/heap/max-heap.ts +0 -31
  39. package/src/data-structures/heap/min-heap.ts +0 -32
  40. package/src/data-structures/index.ts +0 -11
  41. package/src/data-structures/linked-list/doubly-linked-list.ts +0 -636
  42. package/src/data-structures/linked-list/index.ts +0 -3
  43. package/src/data-structures/linked-list/singly-linked-list.ts +0 -501
  44. package/src/data-structures/linked-list/skip-linked-list.ts +0 -166
  45. package/src/data-structures/matrix/index.ts +0 -4
  46. package/src/data-structures/matrix/matrix.ts +0 -27
  47. package/src/data-structures/matrix/matrix2d.ts +0 -213
  48. package/src/data-structures/matrix/navigator.ts +0 -121
  49. package/src/data-structures/matrix/vector2d.ts +0 -316
  50. package/src/data-structures/priority-queue/index.ts +0 -3
  51. package/src/data-structures/priority-queue/max-priority-queue.ts +0 -56
  52. package/src/data-structures/priority-queue/min-priority-queue.ts +0 -57
  53. package/src/data-structures/priority-queue/priority-queue.ts +0 -359
  54. package/src/data-structures/queue/deque.ts +0 -297
  55. package/src/data-structures/queue/index.ts +0 -2
  56. package/src/data-structures/queue/queue.ts +0 -191
  57. package/src/data-structures/stack/index.ts +0 -1
  58. package/src/data-structures/stack/stack.ts +0 -98
  59. package/src/data-structures/tree/index.ts +0 -1
  60. package/src/data-structures/tree/tree.ts +0 -69
  61. package/src/data-structures/trie/index.ts +0 -1
  62. package/src/data-structures/trie/trie.ts +0 -225
  63. package/src/index.ts +0 -4
  64. package/src/interfaces/abstract-binary-tree.ts +0 -189
  65. package/src/interfaces/abstract-graph.ts +0 -31
  66. package/src/interfaces/avl-tree.ts +0 -25
  67. package/src/interfaces/binary-tree.ts +0 -6
  68. package/src/interfaces/bst.ts +0 -31
  69. package/src/interfaces/directed-graph.ts +0 -20
  70. package/src/interfaces/doubly-linked-list.ts +0 -1
  71. package/src/interfaces/heap.ts +0 -1
  72. package/src/interfaces/index.ts +0 -15
  73. package/src/interfaces/navigator.ts +0 -1
  74. package/src/interfaces/priority-queue.ts +0 -1
  75. package/src/interfaces/rb-tree.ts +0 -9
  76. package/src/interfaces/segment-tree.ts +0 -1
  77. package/src/interfaces/singly-linked-list.ts +0 -1
  78. package/src/interfaces/tree-multiset.ts +0 -7
  79. package/src/interfaces/undirected-graph.ts +0 -6
  80. package/src/types/data-structures/abstract-binary-tree.ts +0 -50
  81. package/src/types/data-structures/abstract-graph.ts +0 -11
  82. package/src/types/data-structures/avl-tree.ts +0 -5
  83. package/src/types/data-structures/binary-tree.ts +0 -5
  84. package/src/types/data-structures/bst.ts +0 -13
  85. package/src/types/data-structures/directed-graph.ts +0 -8
  86. package/src/types/data-structures/doubly-linked-list.ts +0 -1
  87. package/src/types/data-structures/hash.ts +0 -1
  88. package/src/types/data-structures/heap.ts +0 -5
  89. package/src/types/data-structures/index.ts +0 -16
  90. package/src/types/data-structures/map-graph.ts +0 -1
  91. package/src/types/data-structures/navigator.ts +0 -13
  92. package/src/types/data-structures/priority-queue.ts +0 -9
  93. package/src/types/data-structures/rb-tree.ts +0 -8
  94. package/src/types/data-structures/segment-tree.ts +0 -1
  95. package/src/types/data-structures/singly-linked-list.ts +0 -1
  96. package/src/types/data-structures/tree-multiset.ts +0 -6
  97. package/src/types/helpers.ts +0 -1
  98. package/src/types/index.ts +0 -3
  99. package/src/types/utils/index.ts +0 -2
  100. package/src/types/utils/utils.ts +0 -6
  101. package/src/types/utils/validate-type.ts +0 -35
  102. package/src/utils/index.ts +0 -1
  103. package/src/utils/utils.ts +0 -79
  104. package/test/integration/avl-tree.test.ts +0 -108
  105. package/test/integration/bst.test.ts +0 -380
  106. package/test/integration/heap.test.js +0 -16
  107. package/test/integration/index.html +0 -52
  108. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +0 -108
  109. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +0 -142
  110. package/test/unit/data-structures/binary-tree/bst.test.ts +0 -380
  111. package/test/unit/data-structures/binary-tree/overall.test.ts +0 -65
  112. package/test/unit/data-structures/binary-tree/rb-tree.test.ts +0 -43
  113. package/test/unit/data-structures/binary-tree/segment-tree.test.ts +0 -50
  114. package/test/unit/data-structures/binary-tree/tree-multiset.test.ts +0 -461
  115. package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -5
  116. package/test/unit/data-structures/graph/directed-graph.test.ts +0 -519
  117. package/test/unit/data-structures/graph/index.ts +0 -2
  118. package/test/unit/data-structures/graph/map-graph.test.ts +0 -45
  119. package/test/unit/data-structures/graph/overall.test.ts +0 -49
  120. package/test/unit/data-structures/graph/undirected-graph.test.ts +0 -59
  121. package/test/unit/data-structures/hash/coordinate-map.test.ts +0 -54
  122. package/test/unit/data-structures/hash/coordinate-set.test.ts +0 -41
  123. package/test/unit/data-structures/hash/hash-map.test.ts +0 -104
  124. package/test/unit/data-structures/hash/hash-table.test.ts +0 -184
  125. package/test/unit/data-structures/heap/heap.test.ts +0 -55
  126. package/test/unit/data-structures/heap/max-heap.test.ts +0 -44
  127. package/test/unit/data-structures/heap/min-heap.test.ts +0 -82
  128. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +0 -364
  129. package/test/unit/data-structures/linked-list/index.ts +0 -4
  130. package/test/unit/data-structures/linked-list/linked-list.test.ts +0 -35
  131. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -451
  132. package/test/unit/data-structures/linked-list/skip-linked-list.test.ts +0 -13
  133. package/test/unit/data-structures/linked-list/skip-list.test.ts +0 -55
  134. package/test/unit/data-structures/matrix/matrix.test.ts +0 -54
  135. package/test/unit/data-structures/matrix/matrix2d.test.ts +0 -138
  136. package/test/unit/data-structures/matrix/navigator.test.ts +0 -79
  137. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +0 -106
  138. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +0 -105
  139. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +0 -27
  140. package/test/unit/data-structures/queue/deque.test.ts +0 -130
  141. package/test/unit/data-structures/queue/queue.test.ts +0 -199
  142. package/test/unit/data-structures/stack/stack.test.ts +0 -67
  143. package/test/unit/data-structures/tree/tree.test.ts +0 -39
  144. package/test/unit/data-structures/trie/trie.test.ts +0 -95
  145. package/test/utils/index.ts +0 -2
  146. package/test/utils/magnitude.ts +0 -21
  147. package/test/utils/number.ts +0 -3
  148. package/tsconfig.json +0 -29
@@ -1,700 +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 type {BinaryTreeNodeId, TreeMultisetNodeNested, TreeMultisetOptions} from '../../types';
9
- import {BinaryTreeDeletedResult, CP, DFSOrderPattern, FamilyPosition, LoopType} from '../../types';
10
- import {ITreeMultiset, ITreeMultisetNode} from '../../interfaces';
11
- import {AVLTree, AVLTreeNode} from './avl-tree';
12
-
13
- export class TreeMultisetNode<V = any, NEIGHBOR extends TreeMultisetNode<V, NEIGHBOR> = TreeMultisetNodeNested<V>>
14
- extends AVLTreeNode<V, NEIGHBOR>
15
- implements ITreeMultisetNode<V, NEIGHBOR>
16
- {
17
- /**
18
- * The constructor function initializes a BinaryTreeNode object with an id, value, and count.
19
- * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
20
- * of the binary tree node.
21
- * @param {V} [val] - The `val` parameter is an optional parameter of type `V`. It represents the value of the binary
22
- * tree node. If no value is provided, it will be `undefined`.
23
- * @param {number} [count=1] - The `count` parameter is a number that represents the number of times a particular value
24
- * occurs in a binary tree node. It has a default value of 1, which means that if no value is provided for the `count`
25
- * parameter when creating a new instance of the `BinaryTreeNode` class,
26
- */
27
- constructor(id: BinaryTreeNodeId, val?: V, count = 1) {
28
- super(id, val);
29
- this._count = count;
30
- }
31
-
32
- private _count: number;
33
-
34
- get count(): number {
35
- return this._count;
36
- }
37
-
38
- set count(v: number) {
39
- this._count = v;
40
- }
41
- }
42
-
43
- /**
44
- * The only distinction between a TreeMultiset and a AVLTree lies in the ability of the former to store duplicate nodes through the utilization of counters.
45
- */
46
- export class TreeMultiset<N extends TreeMultisetNode<N['val'], N> = TreeMultisetNode>
47
- extends AVLTree<N>
48
- implements ITreeMultiset<N>
49
- {
50
- /**
51
- * The constructor function for a TreeMultiset class in TypeScript, which extends another class and sets an option to
52
- * merge duplicated values.
53
- * @param {TreeMultisetOptions} [options] - An optional object that contains additional configuration options for the
54
- * TreeMultiset.
55
- */
56
- constructor(options?: TreeMultisetOptions) {
57
- super({...options});
58
- }
59
-
60
- private _count = 0;
61
-
62
- get count(): number {
63
- return this._count;
64
- }
65
-
66
- /**
67
- * The function creates a new BSTNode with the given id, value, and count.
68
- * @param {BinaryTreeNodeId} id - The id parameter is the unique identifier for the binary tree node. It is used to
69
- * distinguish one node from another in the tree.
70
- * @param {N} val - The `val` parameter represents the value that will be stored in the binary search tree node.
71
- * @param {number} [count] - The "count" parameter is an optional parameter of type number. It represents the number of
72
- * occurrences of the value in the binary search tree node. If not provided, the count will default to 1.
73
- * @returns A new instance of the BSTNode class with the specified id, value, and count (if provided).
74
- */
75
- override createNode(id: BinaryTreeNodeId, val?: N['val'], count?: number): N {
76
- return new TreeMultisetNode(id, val, count) as N;
77
- }
78
-
79
- /**
80
- * The function swaps the location of two nodes in a tree data structure.
81
- * @param {N} srcNode - The source node that we want to swap with the destination node.
82
- * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
83
- * be swapped with.
84
- * @returns the `destNode` after swapping its values with the `srcNode`.
85
- */
86
- override swapLocation(srcNode: N, destNode: N): N {
87
- const {id, val, count, height} = destNode;
88
- const tempNode = this.createNode(id, val, count);
89
- if (tempNode) {
90
- tempNode.height = height;
91
-
92
- destNode.id = srcNode.id;
93
- destNode.val = srcNode.val;
94
- destNode.count = srcNode.count;
95
- destNode.height = srcNode.height;
96
-
97
- srcNode.id = tempNode.id;
98
- srcNode.val = tempNode.val;
99
- srcNode.count = tempNode.count;
100
- srcNode.height = tempNode.height;
101
- }
102
-
103
- return destNode;
104
- }
105
-
106
- /**
107
- * The `add` function adds a new node to a binary search tree, maintaining the tree's properties and balancing if
108
- * necessary.
109
- * @param {BinaryTreeNodeId | N} idOrNode - The `idOrNode` parameter can be either a `BinaryTreeNodeId` or a `N` (which
110
- * represents a `BinaryTreeNode`).
111
- * @param [val] - The `val` parameter represents the value to be added to the binary tree node.
112
- * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
113
- * value should be added to the binary tree. If the `count` parameter is not provided, it defaults to 1.
114
- * @returns The method `add` returns either the inserted node (`N`), `null`, or `undefined`.
115
- */
116
- override add(idOrNode: BinaryTreeNodeId | N | null, val?: N['val'], count?: number): N | null | undefined {
117
- count = count ?? 1;
118
- let inserted: N | null | undefined = undefined,
119
- newNode: N | null;
120
- if (idOrNode instanceof TreeMultisetNode) {
121
- newNode = this.createNode(idOrNode.id, idOrNode.val, idOrNode.count);
122
- } else if (idOrNode === null) {
123
- newNode = null;
124
- } else {
125
- newNode = this.createNode(idOrNode, val, count);
126
- }
127
- if (!this.root) {
128
- this._setRoot(newNode);
129
- this._setSize(this.size + 1);
130
- newNode && this._setCount(this.count + newNode.count);
131
- inserted = this.root;
132
- } else {
133
- let cur = this.root;
134
- let traversing = true;
135
- while (traversing) {
136
- if (cur) {
137
- if (newNode) {
138
- if (this._compare(cur.id, newNode.id) === CP.eq) {
139
- cur.val = newNode.val;
140
- cur.count += newNode.count;
141
- this._setCount(this.count + newNode.count);
142
- traversing = false;
143
- inserted = cur;
144
- } else if (this._compare(cur.id, newNode.id) === CP.gt) {
145
- // Traverse left of the node
146
- if (cur.left === undefined) {
147
- //Add to the left of the current node
148
- cur.left = newNode;
149
- this._setSize(this.size + 1);
150
- this._setCount(this.count + newNode.count);
151
-
152
- traversing = false;
153
- inserted = cur.left;
154
- } else {
155
- //Traverse the left of the current node
156
- if (cur.left) cur = cur.left;
157
- }
158
- } else if (this._compare(cur.id, newNode.id) === CP.lt) {
159
- // Traverse right of the node
160
- if (cur.right === undefined) {
161
- //Add to the right of the current node
162
- cur.right = newNode;
163
- this._setSize(this.size + 1);
164
- this._setCount(this.count + newNode.count);
165
-
166
- traversing = false;
167
- inserted = cur.right;
168
- } else {
169
- //Traverse the left of the current node
170
- if (cur.right) cur = cur.right;
171
- }
172
- }
173
- } else {
174
- // TODO may need to support null inserted
175
- }
176
- } else {
177
- traversing = false;
178
- }
179
- }
180
- }
181
- if (inserted) this._balancePath(inserted);
182
- return inserted;
183
- }
184
-
185
- /**
186
- * The function adds a new node to a binary tree if there is an available slot on the left or right side of the parent
187
- * node.
188
- * @param {N | null} newNode - The `newNode` parameter represents the node that needs to be added to the tree. It can
189
- * be either a node object (`N`) or `null`.
190
- * @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
191
- * child.
192
- * @returns The method returns either the `parent.left`, `parent.right`, or `undefined`.
193
- */
194
- override _addTo(newNode: N | null, parent: N): N | null | undefined {
195
- if (parent) {
196
- if (parent.left === undefined) {
197
- parent.left = newNode;
198
- if (newNode !== null) {
199
- this._setSize(this.size + 1);
200
- this._setCount(this.count + newNode.count);
201
- }
202
-
203
- return parent.left;
204
- } else if (parent.right === undefined) {
205
- parent.right = newNode;
206
- if (newNode !== null) {
207
- this._setSize(this.size + 1);
208
- this._setCount(this.count + newNode.count);
209
- }
210
- return parent.right;
211
- } else {
212
- return;
213
- }
214
- } else {
215
- return;
216
- }
217
- }
218
-
219
- /**
220
- * The `addMany` function takes an array of node IDs or nodes and adds them to the tree multiset, returning an array of
221
- * the inserted nodes.
222
- * @param {(BinaryTreeNodeId | null)[] | (N | null)[]} idsOrNodes - An array of BinaryTreeNodeId or BinaryTreeNode
223
- * objects, or null values.
224
- * @param {N['val'][]} [data] - The `data` parameter is an optional array of values (`N['val'][]`) that corresponds to
225
- * the nodes being added. It is used when adding nodes using the `idOrNode` and `data` arguments in the `this.add()`
226
- * method. If provided, the `data` array should
227
- * @returns The function `addMany` returns an array of `N`, `null`, or `undefined` values.
228
- */
229
- override addMany(
230
- idsOrNodes: (BinaryTreeNodeId | null)[] | (N | null)[],
231
- data?: N['val'][]
232
- ): (N | null | undefined)[] {
233
- const inserted: (N | null | undefined)[] = [];
234
-
235
- for (let i = 0; i < idsOrNodes.length; i++) {
236
- const idOrNode = idsOrNodes[i];
237
-
238
- if (idOrNode instanceof TreeMultisetNode) {
239
- inserted.push(this.add(idOrNode.id, idOrNode.val, idOrNode.count));
240
- continue;
241
- }
242
-
243
- if (idOrNode === null) {
244
- inserted.push(this.add(NaN, null, 0));
245
- continue;
246
- }
247
-
248
- inserted.push(this.add(idOrNode, data?.[i], 1));
249
- }
250
- return inserted;
251
- }
252
-
253
- /**
254
- * The `perfectlyBalance` function takes a binary tree, performs a depth-first search to sort the nodes, and then
255
- * constructs a balanced binary search tree using either a recursive or iterative approach.
256
- * @returns The function `perfectlyBalance()` returns a boolean value.
257
- */
258
- override perfectlyBalance(): boolean {
259
- const sorted = this.DFS('in', 'node'),
260
- n = sorted.length;
261
- if (sorted.length < 1) return false;
262
-
263
- this.clear();
264
-
265
- if (this.loopType === LoopType.RECURSIVE) {
266
- const buildBalanceBST = (l: number, r: number) => {
267
- if (l > r) return;
268
- const m = l + Math.floor((r - l) / 2);
269
- const midNode = sorted[m];
270
- this.add(midNode.id, midNode.val, midNode.count);
271
- buildBalanceBST(l, m - 1);
272
- buildBalanceBST(m + 1, r);
273
- };
274
-
275
- buildBalanceBST(0, n - 1);
276
- return true;
277
- } else {
278
- const stack: [[number, number]] = [[0, n - 1]];
279
- while (stack.length > 0) {
280
- const popped = stack.pop();
281
- if (popped) {
282
- const [l, r] = popped;
283
- if (l <= r) {
284
- const m = l + Math.floor((r - l) / 2);
285
- const midNode = sorted[m];
286
- this.add(midNode.id, midNode.val, midNode.count);
287
- stack.push([m + 1, r]);
288
- stack.push([l, m - 1]);
289
- }
290
- }
291
- }
292
- return true;
293
- }
294
- }
295
-
296
- /**
297
- * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
298
- * node that needs to be balanced.
299
- * @param {N | BinaryTreeNodeId | null} nodeOrId - The `nodeOrId` parameter can be one of the following:
300
- * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
301
- * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
302
- * not be taken into account when removing it. If `ignoreCount` is set to `false
303
- * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
304
- */
305
- override remove(nodeOrId: N | BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[] {
306
- const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
307
- if (!this.root) return bstDeletedResult;
308
-
309
- const curr: N | null = this.get(nodeOrId);
310
- if (!curr) return bstDeletedResult;
311
-
312
- const parent: N | null = curr?.parent ? curr.parent : null;
313
- let needBalanced: N | null = null,
314
- orgCurrent = curr;
315
-
316
- if (curr.count > 1 && !ignoreCount) {
317
- curr.count--;
318
- this._setCount(this.count - 1);
319
- } else {
320
- if (!curr.left) {
321
- if (!parent) {
322
- if (curr.right !== undefined) this._setRoot(curr.right);
323
- } else {
324
- const {familyPosition: fp} = curr;
325
- if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
326
- parent.left = curr.right;
327
- } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
328
- parent.right = curr.right;
329
- }
330
- needBalanced = parent;
331
- }
332
- } else {
333
- const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
334
- if (leftSubTreeRightMost) {
335
- const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
336
- orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
337
- if (parentOfLeftSubTreeMax) {
338
- if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) {
339
- parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
340
- } else {
341
- parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
342
- }
343
- needBalanced = parentOfLeftSubTreeMax;
344
- }
345
- }
346
- }
347
- this._setSize(this.size - 1);
348
- // TODO How to handle when the count of target node is lesser than current node's count
349
- this._setCount(this.count - orgCurrent.count);
350
- }
351
-
352
- bstDeletedResult.push({deleted: orgCurrent, needBalanced});
353
-
354
- if (needBalanced) {
355
- this._balancePath(needBalanced);
356
- }
357
-
358
- return bstDeletedResult;
359
- }
360
-
361
- /**
362
- * The function `getSubTreeCount` calculates the number of nodes and the sum of their counts in a subtree, using either
363
- * recursive or iterative traversal.
364
- * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
365
- * binary tree.
366
- * @returns The function `getSubTreeCount` returns an array `[number, number]`.
367
- */
368
- getSubTreeCount(subTreeRoot: N | null | undefined) {
369
- const res: [number, number] = [0, 0];
370
- if (!subTreeRoot) return res;
371
-
372
- if (this.loopType === LoopType.RECURSIVE) {
373
- const _traverse = (cur: N) => {
374
- res[0]++;
375
- res[1] += cur.count;
376
- cur.left && _traverse(cur.left);
377
- cur.right && _traverse(cur.right);
378
- };
379
-
380
- _traverse(subTreeRoot);
381
- return res;
382
- } else {
383
- const stack: N[] = [subTreeRoot];
384
-
385
- while (stack.length > 0) {
386
- const cur = stack.pop()!;
387
- res[0]++;
388
- res[1] += cur.count;
389
- cur.right && stack.push(cur.right);
390
- cur.left && stack.push(cur.left);
391
- }
392
-
393
- return res;
394
- }
395
- }
396
-
397
- /**
398
- * The function `subTreeSumCount` calculates the sum of the `count` property of each node in a subtree, either
399
- * recursively or iteratively.
400
- * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
401
- * in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree) or
402
- * `null` if the subtree is empty.
403
- * @returns the sum of the count values of all nodes in the subtree rooted at `subTreeRoot`.
404
- */
405
- subTreeSumCount(subTreeRoot: N | BinaryTreeNodeId | null): number {
406
- if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
407
-
408
- if (!subTreeRoot) return 0;
409
-
410
- let sum = 0;
411
-
412
- if (this.loopType === LoopType.RECURSIVE) {
413
- const _traverse = (cur: N): void => {
414
- sum += cur.count;
415
- cur.left && _traverse(cur.left);
416
- cur.right && _traverse(cur.right);
417
- };
418
-
419
- _traverse(subTreeRoot);
420
- } else {
421
- const stack: N[] = [subTreeRoot];
422
-
423
- while (stack.length > 0) {
424
- const cur = stack.pop()!;
425
- sum += cur.count;
426
- cur.right && stack.push(cur.right);
427
- cur.left && stack.push(cur.left);
428
- }
429
- }
430
-
431
- return sum;
432
- }
433
-
434
- /**
435
- * The function `subTreeAddCount` recursively or iteratively traverses a binary tree and adds a given delta value to
436
- * the `count` property of each node.
437
- * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree
438
- * in a binary tree. It can be either a `BinaryTreeNodeId` (a unique identifier for a node in the binary tree), a
439
- * `BinaryTreeNode` object, or `null` if the subtree is empty.
440
- * @param {number} delta - The delta parameter is a number that represents the amount by which the count of each node
441
- * in the subtree should be increased or decreased.
442
- * @returns a boolean value.
443
- */
444
- subTreeAddCount(subTreeRoot: N | BinaryTreeNodeId | null, delta: number): boolean {
445
- if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
446
-
447
- if (!subTreeRoot) return false;
448
-
449
- const _addByProperty = (cur: N) => {
450
- cur.count += delta;
451
- this._setCount(this.count + delta);
452
- };
453
-
454
- if (this.loopType === LoopType.RECURSIVE) {
455
- const _traverse = (cur: N) => {
456
- _addByProperty(cur);
457
- cur.left && _traverse(cur.left);
458
- cur.right && _traverse(cur.right);
459
- };
460
-
461
- _traverse(subTreeRoot);
462
- } else {
463
- const stack: N[] = [subTreeRoot];
464
-
465
- while (stack.length > 0) {
466
- const cur = stack.pop()!;
467
-
468
- _addByProperty(cur);
469
- cur.right && stack.push(cur.right);
470
- cur.left && stack.push(cur.left);
471
- }
472
- }
473
- return true;
474
- }
475
-
476
- /**
477
- * The function `getNodesByCount` returns an array of nodes that have a specific count property, either recursively or
478
- * using a queue.
479
- * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
480
- * `N`. It represents the property of the nodes that you want to search for.
481
- * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
482
- * return only one node that matches the `nodeProperty` or all nodes that match the `nodeProperty`. If `onlyOne` is set
483
- * to `true`, the function will return only one node. If `onlyOne`
484
- * @returns an array of nodes that match the given nodeProperty.
485
- */
486
- getNodesByCount(nodeProperty: BinaryTreeNodeId | N, onlyOne?: boolean): N[] {
487
- if (!this.root) return [];
488
- const result: N[] = [];
489
-
490
- if (this.loopType === LoopType.RECURSIVE) {
491
- const _traverse = (cur: N) => {
492
- if (cur.count === nodeProperty) {
493
- result.push(cur);
494
- if (onlyOne) return;
495
- }
496
-
497
- if (!cur.left && !cur.right) return;
498
- cur.left && _traverse(cur.left);
499
- cur.right && _traverse(cur.right);
500
- };
501
-
502
- _traverse(this.root);
503
- } else {
504
- const queue: N[] = [this.root];
505
- while (queue.length > 0) {
506
- const cur = queue.shift();
507
- if (cur) {
508
- if (cur.count === nodeProperty) {
509
- result.push(cur);
510
- if (onlyOne) return result;
511
- }
512
-
513
- cur.left && queue.push(cur.left);
514
- cur.right && queue.push(cur.right);
515
- }
516
- }
517
- }
518
-
519
- return result;
520
- }
521
-
522
- /**
523
- * The BFSCount function returns an array of counts from a breadth-first search of nodes.
524
- * @returns The BFSCount() function returns an array of numbers, specifically the count property of each node in the
525
- * BFS traversal.
526
- */
527
- BFSCount(): number[] {
528
- const nodes = super.BFS('node');
529
- return nodes.map(node => node.count);
530
- }
531
-
532
- /**
533
- * The function "listLevelsCount" takes a node and returns an array of arrays, where each inner array contains the
534
- * count property of each node at that level.
535
- * @param {N | null} node - The parameter `node` is of type `N | null`. This means that it can either be an instance of
536
- * the class `N` or `null`.
537
- * @returns a 2D array of numbers. Each inner array represents a level in the binary tree, and each number in the inner
538
- * array represents the count property of a node in that level.
539
- */
540
- listLevelsCount(node: N | null): number[][] {
541
- const levels = super.listLevels(node, 'node');
542
- return levels.map(level => level.map(node => node.count));
543
- }
544
-
545
- /**
546
- * The `morrisCount` function returns an array of counts for each node in a binary tree, based on a specified traversal
547
- * pattern.
548
- * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that specifies the
549
- * traversal pattern for the Morris traversal algorithm. It can have one of three values: 'in', 'pre', or 'post'.
550
- * @returns The function `morrisCount` returns an array of numbers.
551
- */
552
- morrisCount(pattern?: 'in' | 'pre' | 'post'): number[] {
553
- pattern = pattern || 'in';
554
- const nodes = super.morris(pattern, 'node');
555
- return nodes.map(node => node.count);
556
- }
557
-
558
- /**
559
- * The function DFSIterativeCount performs an iterative depth-first search and returns an array of node counts based on
560
- * the specified traversal pattern.
561
- * @param {'in' | 'pre' | 'post'} [pattern] - The pattern parameter is a string that specifies the traversal order for
562
- * the Depth-First Search (DFS) algorithm. It can have three possible values: 'in', 'pre', or 'post'.
563
- * @returns The DFSIterativeCount function returns an array of numbers, which represents the count property of each node
564
- * in the DFS traversal.
565
- */
566
- DFSIterativeCount(pattern?: 'in' | 'pre' | 'post'): number[] {
567
- pattern = pattern ?? 'in';
568
- const nodes = super.DFSIterative(pattern, 'node');
569
- return nodes.map(node => node.count);
570
- }
571
-
572
- /**
573
- * The DFSCount function returns an array of counts for each node in a depth-first search traversal.
574
- * @param {DFSOrderPattern} [pattern] - The pattern parameter is an optional parameter that specifies the order in which
575
- * the Depth-First Search (DFS) algorithm should traverse the nodes. It can have one of the following values:
576
- * @returns The DFSCount function returns an array of numbers, specifically the count property of each node in the DFS
577
- * traversal.
578
- */
579
- DFSCount(pattern?: DFSOrderPattern): number[] {
580
- pattern = pattern ?? 'in';
581
- const nodes = super.DFS(pattern, 'node');
582
- return nodes.map(node => node.count);
583
- }
584
-
585
- /**
586
- * The `lesserSumCount` function calculates the sum of the counts of all nodes in a binary tree that have a lesser
587
- * value than a given node.
588
- * @param {N | BinaryTreeNodeId | null} beginNode - The `beginNode` parameter can be one of the following:
589
- * @returns the sum of the counts of nodes in the binary tree that have a lesser value than the given beginNode.
590
- */
591
- lesserSumCount(beginNode: N | BinaryTreeNodeId | null): number {
592
- if (typeof beginNode === 'number') beginNode = this.get(beginNode, 'id');
593
- if (!beginNode) return 0;
594
- if (!this.root) return 0;
595
- const id = beginNode.id;
596
-
597
- let sum = 0;
598
-
599
- if (this.loopType === LoopType.RECURSIVE) {
600
- const _traverse = (cur: N): void => {
601
- const compared = this._compare(cur.id, id);
602
- if (compared === CP.eq) {
603
- if (cur.right) sum += this.subTreeSumCount(cur.right);
604
- return;
605
- } else if (compared === CP.lt) {
606
- if (cur.left) sum += this.subTreeSumCount(cur.left);
607
- sum += cur.count;
608
- if (cur.right) _traverse(cur.right);
609
- else return;
610
- } else {
611
- if (cur.left) _traverse(cur.left);
612
- else return;
613
- }
614
- };
615
-
616
- _traverse(this.root);
617
- } else {
618
- const queue: N[] = [this.root];
619
- while (queue.length > 0) {
620
- const cur = queue.shift();
621
- if (cur) {
622
- const compared = this._compare(cur.id, id);
623
- if (compared === CP.eq) {
624
- if (cur.right) sum += this.subTreeSumCount(cur.right);
625
- return sum;
626
- } else if (compared === CP.lt) {
627
- // todo maybe a bug
628
- if (cur.left) sum += this.subTreeSumCount(cur.left);
629
- sum += cur.count;
630
- if (cur.right) queue.push(cur.right);
631
- else return sum;
632
- } else {
633
- if (cur.left) queue.push(cur.left);
634
- else return sum;
635
- }
636
- }
637
- }
638
- }
639
-
640
- return sum;
641
- }
642
-
643
- /**
644
- * The function `allGreaterNodesAddCount` updates the count property of all nodes in a binary tree that have an ID
645
- * greater than a given ID by a specified delta value.
646
- * @param {N | BinaryTreeNodeId | null} node - The `node` parameter can be one of the following:
647
- * @param {number} delta - The `delta` parameter is a number that represents the amount by which the `count` property
648
- * of each node should be increased.
649
- * @returns a boolean value.
650
- */
651
- allGreaterNodesAddCount(node: N | BinaryTreeNodeId | null, delta: number): boolean {
652
- if (typeof node === 'number') node = this.get(node, 'id');
653
- if (!node) return false;
654
- const id = node.id;
655
- if (!this.root) return false;
656
-
657
- if (this.loopType === LoopType.RECURSIVE) {
658
- const _traverse = (cur: N) => {
659
- const compared = this._compare(cur.id, id);
660
- if (compared === CP.gt) cur.count += delta;
661
-
662
- if (!cur.left && !cur.right) return;
663
- if (cur.left && this._compare(cur.left.id, id) === CP.gt) _traverse(cur.left);
664
- if (cur.right && this._compare(cur.right.id, id) === CP.gt) _traverse(cur.right);
665
- };
666
-
667
- _traverse(this.root);
668
- return true;
669
- } else {
670
- const queue: N[] = [this.root];
671
- while (queue.length > 0) {
672
- const cur = queue.shift();
673
- if (cur) {
674
- const compared = this._compare(cur.id, id);
675
- if (compared === CP.gt) cur.count += delta;
676
-
677
- if (cur.left && this._compare(cur.left.id, id) === CP.gt) queue.push(cur.left);
678
- if (cur.right && this._compare(cur.right.id, id) === CP.gt) queue.push(cur.right);
679
- }
680
- }
681
- return true;
682
- }
683
- }
684
-
685
- /**
686
- * The clear() function clears the data and sets the count to 0.
687
- */
688
- clear() {
689
- super.clear();
690
- this._setCount(0);
691
- }
692
-
693
- /**
694
- * The function "_setCount" is used to set the value of the "_count" property.
695
- * @param {number} v - number
696
- */
697
- protected _setCount(v: number) {
698
- this._count = v;
699
- }
700
- }
@@ -1 +0,0 @@
1
- export class TwoThreeTree {}