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