data-structure-typed 1.19.3 → 1.19.5

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 +3 -2
  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,1528 @@
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 {ObjectWithNumberId, trampoline} from '../../utils';
10
+ import type {
11
+ AbstractBinaryTreeNodeNested,
12
+ AbstractBinaryTreeNodeProperties,
13
+ AbstractBinaryTreeNodeProperty,
14
+ BinaryTreeDeletedResult,
15
+ BinaryTreeNodeId,
16
+ BinaryTreeNodePropertyName,
17
+ DFSOrderPattern,
18
+ NodeOrPropertyName
19
+ } from '../types';
20
+ import {AbstractBinaryTreeOptions, FamilyPosition, LoopType} from '../types';
21
+ import {IAbstractBinaryTree, IAbstractBinaryTreeNode} from '../interfaces';
22
+
23
+ export abstract class AbstractBinaryTreeNode<T = any, NEIGHBOR extends AbstractBinaryTreeNode<T, NEIGHBOR> = AbstractBinaryTreeNodeNested<T>> implements IAbstractBinaryTreeNode<T, NEIGHBOR> {
24
+
25
+
26
+ /**
27
+ * The constructor function initializes a BinaryTreeNode object with an id and an optional value.
28
+ * @param {BinaryTreeNodeId} id - The `id` parameter is of type `BinaryTreeNodeId` and represents the unique identifier
29
+ * of the binary tree node. It is used to distinguish one node from another in the binary tree.
30
+ * @param {T} [val] - The "val" parameter is an optional parameter of type T. It represents the value that will be
31
+ * stored in the binary tree node. If no value is provided, it will be set to undefined.
32
+ */
33
+ constructor(id: BinaryTreeNodeId, val?: T) {
34
+ this._id = id;
35
+ this._val = val;
36
+ }
37
+
38
+ private _id: BinaryTreeNodeId;
39
+
40
+ get id(): BinaryTreeNodeId {
41
+ return this._id;
42
+ }
43
+
44
+ set id(v: BinaryTreeNodeId) {
45
+ this._id = v;
46
+ }
47
+
48
+ private _val: T | undefined;
49
+
50
+ get val(): T | undefined {
51
+ return this._val;
52
+ }
53
+
54
+ set val(value: T | undefined) {
55
+ this._val = value;
56
+ }
57
+
58
+ private _left: NEIGHBOR | null | undefined;
59
+
60
+ get left(): NEIGHBOR | null | undefined {
61
+ return this._left;
62
+ }
63
+
64
+ set left(v: NEIGHBOR | null | undefined) {
65
+ if (v) {
66
+ v.parent = this as unknown as NEIGHBOR;
67
+ }
68
+ this._left = v;
69
+ }
70
+
71
+ private _right: NEIGHBOR | null | undefined;
72
+
73
+ get right(): NEIGHBOR | null | undefined {
74
+ return this._right;
75
+ }
76
+
77
+ set right(v: NEIGHBOR | null | undefined) {
78
+ if (v) {
79
+ v.parent = this as unknown as NEIGHBOR;
80
+ }
81
+ this._right = v;
82
+ }
83
+
84
+ private _parent: NEIGHBOR | null | undefined;
85
+
86
+ get parent(): NEIGHBOR | null | undefined {
87
+ return this._parent;
88
+ }
89
+
90
+ set parent(v: NEIGHBOR | null | undefined) {
91
+ this._parent = v;
92
+ }
93
+
94
+ private _height = 0;
95
+
96
+ get height(): number {
97
+ return this._height;
98
+ }
99
+
100
+ set height(v: number) {
101
+ this._height = v;
102
+ }
103
+
104
+ /**
105
+ * The function determines the position of a node in a family tree structure.
106
+ * @returns a value of type `FamilyPosition`.
107
+ */
108
+ get familyPosition(): FamilyPosition {
109
+ const that = this as unknown as NEIGHBOR;
110
+ if (that.parent) {
111
+ if (that.parent.left === that) {
112
+ if (that.left || that.right) {
113
+ return FamilyPosition.ROOT_LEFT;
114
+ } else {
115
+ return FamilyPosition.LEFT;
116
+ }
117
+ } else if (that.parent.right === that) {
118
+ if (that.left || that.right) {
119
+ return FamilyPosition.ROOT_RIGHT;
120
+ } else {
121
+ return FamilyPosition.RIGHT;
122
+ }
123
+ } else {
124
+ return FamilyPosition.MAL_NODE;
125
+ }
126
+ } else {
127
+ if (that.left || that.right) {
128
+ return FamilyPosition.ROOT;
129
+ } else {
130
+ return FamilyPosition.ISOLATED;
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ export abstract class AbstractBinaryTree<N extends AbstractBinaryTreeNode<N['val'], N> = AbstractBinaryTreeNode> implements IAbstractBinaryTree<N> {
137
+
138
+ /**
139
+ * The protected constructor initializes the options for an abstract binary tree.
140
+ * @param {AbstractBinaryTreeOptions} [options] - An optional object that contains configuration options for the binary
141
+ * tree.
142
+ */
143
+ protected constructor(options?: AbstractBinaryTreeOptions) {
144
+ if (options !== undefined) {
145
+ const {
146
+ loopType = LoopType.ITERATIVE,
147
+ autoIncrementId = false,
148
+ isMergeDuplicatedVal = true
149
+ } = options;
150
+ this._isMergeDuplicatedVal = isMergeDuplicatedVal;
151
+ this._autoIncrementId = autoIncrementId;
152
+ this._loopType = loopType;
153
+ }
154
+ }
155
+
156
+ private _root: N | null = null;
157
+
158
+ get root(): N | null {
159
+ return this._root;
160
+ }
161
+
162
+ private _size = 0;
163
+
164
+ get size(): number {
165
+ return this._size;
166
+ }
167
+
168
+ private _loopType: LoopType = LoopType.ITERATIVE;
169
+
170
+ get loopType(): LoopType {
171
+ return this._loopType;
172
+ }
173
+
174
+ private _autoIncrementId: boolean = false;
175
+
176
+ get autoIncrementId(): boolean {
177
+ return this._autoIncrementId;
178
+ }
179
+
180
+ private _maxId: number = -1;
181
+
182
+ get maxId(): number {
183
+ return this._maxId;
184
+ }
185
+
186
+ // TODO this variable may be moved to TreeMultiset
187
+ private _isMergeDuplicatedVal: boolean = true;
188
+
189
+ get isMergeDuplicatedVal(): boolean {
190
+ return this._isMergeDuplicatedVal;
191
+ }
192
+
193
+ private _visitedId: BinaryTreeNodeId[] = [];
194
+
195
+ get visitedId(): BinaryTreeNodeId[] {
196
+ return this._visitedId;
197
+ }
198
+
199
+ private _visitedVal: Array<N['val']> = [];
200
+
201
+ get visitedVal(): Array<N['val']> {
202
+ return this._visitedVal;
203
+ }
204
+
205
+ private _visitedNode: N[] = [];
206
+
207
+ get visitedNode(): N[] {
208
+ return this._visitedNode;
209
+ }
210
+
211
+ private _visitedCount: number[] = [];
212
+
213
+ get visitedCount(): number[] {
214
+ return this._visitedCount;
215
+ }
216
+
217
+ private _visitedLeftSum: number[] = [];
218
+
219
+ get visitedLeftSum(): number[] {
220
+ return this._visitedLeftSum;
221
+ }
222
+
223
+ abstract createNode(id: BinaryTreeNodeId, val?: N['val']): N | null ;
224
+
225
+ /**
226
+ * The `swapLocation` function swaps the location of two nodes in a binary tree.
227
+ * @param {N} srcNode - The source node that you want to swap with the destination node.
228
+ * @param {N} destNode - The `destNode` parameter represents the destination node where the values from `srcNode` will
229
+ * be swapped to.
230
+ * @returns The `destNode` is being returned.
231
+ */
232
+ swapLocation(srcNode: N, destNode: N): N {
233
+ const {val, height, id} = destNode;
234
+ const tempNode = this.createNode(id, val);
235
+ if (tempNode) {
236
+ tempNode.height = height;
237
+
238
+ if (tempNode instanceof AbstractBinaryTreeNode) {
239
+ // TODO should we consider the left, right children?
240
+ destNode.id = srcNode.id;
241
+ destNode.val = srcNode.val;
242
+ destNode.height = srcNode.height;
243
+
244
+ srcNode.id = tempNode.id;
245
+ srcNode.val = tempNode.val;
246
+ srcNode.height = tempNode.height;
247
+ }
248
+ }
249
+
250
+ return destNode;
251
+ }
252
+
253
+ /**
254
+ * The clear() function resets the root, size, and maxId properties to their initial values.
255
+ */
256
+ clear() {
257
+ this._setRoot(null);
258
+ this._setSize(0);
259
+ this._setMaxId(-1);
260
+ }
261
+
262
+ /**
263
+ * The function checks if the size of an object is equal to zero and returns a boolean value.
264
+ * @returns A boolean value indicating whether the size of the object is 0 or not.
265
+ */
266
+ isEmpty(): boolean {
267
+ return this.size === 0;
268
+ }
269
+
270
+ /**
271
+ * The `add` function adds a new node to a binary tree, updating the value of an existing node if it already exists.
272
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to add.
273
+ * @param [val] - The `val` parameter is an optional value that can be assigned to the node being added. If no value is
274
+ * provided, the default value will be the same as the `id` parameter.
275
+ * @param {number} [count] - The `count` parameter is an optional number that represents the number of times the value
276
+ * should be added to the binary tree. If not provided, the default value is `undefined`.
277
+ * @returns The function `add` returns either a `BinaryTreeNode` object (`N`), `null`, or `undefined`.
278
+ */
279
+ add(id: BinaryTreeNodeId, val?: N['val'], count?: number): N | null | undefined {
280
+ const _bfs = (root: N, newNode: N | null): N | undefined | null => {
281
+ const queue: Array<N | null> = [root];
282
+ while (queue.length > 0) {
283
+ const cur = queue.shift();
284
+ if (cur) {
285
+ const inserted = this.addTo(newNode, cur);
286
+ if (inserted !== undefined) return inserted;
287
+ if (cur.left) queue.push(cur.left);
288
+ if (cur.right) queue.push(cur.right);
289
+ } else return;
290
+ }
291
+ return;
292
+ };
293
+
294
+ let inserted: N | null | undefined;
295
+ const needInsert = val !== null ? this.createNode(id, val) : null;
296
+ const existNode = val !== null ? this.get(id, 'id') : null;
297
+ if (this.root) {
298
+ if (existNode) {
299
+ existNode.val = val ?? id;
300
+ if (needInsert !== null) {
301
+ inserted = existNode;
302
+ }
303
+ } else {
304
+ inserted = _bfs(this.root, needInsert);
305
+ }
306
+ } else {
307
+ this._setRoot(val !== null ? this.createNode(id, val) : null);
308
+ if (needInsert !== null) {
309
+ this._setSize(1);
310
+ }
311
+ inserted = this.root;
312
+ }
313
+ return inserted;
314
+ }
315
+
316
+ /**
317
+ * The function adds a new node to the left or right child of a parent node, updating the size of the tree if
318
+ * necessary.
319
+ * @param {N | null} newNode - The `newNode` parameter represents the node that you want to add to the tree. It can be
320
+ * either a node object (`N`) or `null`.
321
+ * @param {N} parent - The `parent` parameter represents the parent node to which the new node will be added as a
322
+ * child.
323
+ * @returns either the left child node, the right child node, or undefined.
324
+ */
325
+ addTo(newNode: N | null, parent: N): N | null | undefined {
326
+ if (parent) {
327
+ if (parent.left === undefined) {
328
+ if (newNode) {
329
+ newNode.parent = parent;
330
+ }
331
+ parent.left = newNode;
332
+ if (newNode !== null) {
333
+ this._setSize(this.size + 1);
334
+ }
335
+
336
+ return parent.left;
337
+ } else if (parent.right === undefined) {
338
+ if (newNode) {
339
+ newNode.parent = parent;
340
+ }
341
+ parent.right = newNode;
342
+ if (newNode !== null) {
343
+ this._setSize(this.size + 1);
344
+ }
345
+ return parent.right;
346
+ } else {
347
+ return;
348
+ }
349
+ } else {
350
+ return;
351
+ }
352
+ }
353
+
354
+ /**
355
+ * The `addMany` function adds multiple nodes to a binary tree and returns an array of the inserted nodes or
356
+ * null/undefined values.
357
+ * @param {N[] | Array<N['val']>} data - The `data` parameter can be either an array of `N` objects or an array of
358
+ * `N['val']` values.
359
+ * @returns The function `addMany` returns an array of values of type `N | null | undefined`.
360
+ */
361
+ addMany(data: N[] | Array<N['val']>): (N | null | undefined)[] {
362
+ // TODO not sure addMany not be run multi times
363
+ const inserted: (N | null | undefined)[] = [];
364
+ const map: Map<N | N['val'], number> = new Map();
365
+
366
+ if (this.isMergeDuplicatedVal) {
367
+ for (const nodeOrId of data) map.set(nodeOrId, (map.get(nodeOrId) ?? 0) + 1);
368
+ }
369
+
370
+ for (const nodeOrId of data) {
371
+
372
+ if (nodeOrId instanceof AbstractBinaryTreeNode) {
373
+ inserted.push(this.add(nodeOrId.id, nodeOrId.val));
374
+ continue;
375
+ }
376
+
377
+ if (nodeOrId === null) {
378
+ inserted.push(this.add(NaN, null, 0));
379
+ continue;
380
+ }
381
+
382
+ // TODO will this cause an issue?
383
+ const count = this.isMergeDuplicatedVal ? map.get(nodeOrId) : 1;
384
+ let newId: BinaryTreeNodeId;
385
+ if (typeof nodeOrId === 'number') {
386
+ newId = this.autoIncrementId ? this.maxId + 1 : nodeOrId;
387
+ } else if (nodeOrId instanceof Object) {
388
+ if (this.autoIncrementId) {
389
+ newId = this.maxId + 1;
390
+ } else {
391
+ if (Object.keys(nodeOrId).includes('id')) {
392
+ newId = (nodeOrId as ObjectWithNumberId).id;
393
+ } else {
394
+ console.warn(nodeOrId, 'Object value must has an id property when the autoIncrementId is false');
395
+ continue;
396
+ }
397
+ }
398
+ } else {
399
+ console.warn(nodeOrId, ` is not added`);
400
+ continue;
401
+ }
402
+
403
+ if (this.isMergeDuplicatedVal) {
404
+ if (map.has(nodeOrId)) {
405
+ inserted.push(this.add(newId, nodeOrId, count));
406
+ map.delete(nodeOrId);
407
+ }
408
+ } else {
409
+ inserted.push(this.add(newId, nodeOrId, 1));
410
+ }
411
+
412
+ this._setMaxId(newId);
413
+ }
414
+ return inserted;
415
+ }
416
+
417
+ /**
418
+ * The `fill` function clears the current data and adds new data, returning a boolean indicating if the operation was
419
+ * successful.
420
+ * @param {N[] | Array<N['val']>} data - The `data` parameter can be either an array of objects or an array of arrays.
421
+ * Each object or array should have a property called `val`.
422
+ * @returns a boolean value.
423
+ */
424
+ fill(data: N[] | Array<N['val']>): boolean {
425
+ this.clear();
426
+ return data.length === this.addMany(data).length;
427
+ }
428
+
429
+ /**
430
+ * The `remove` function removes a node from a binary search tree and returns the deleted node along with the parent
431
+ * node that needs to be balanced.
432
+ * @param {N | BinaryTreeNodeId} nodeOrId - The `nodeOrId` parameter can be either a node object (`N`) or a binary tree
433
+ * node ID (`BinaryTreeNodeId`).
434
+ * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
435
+ * whether to ignore the count of the nodes in the binary tree. If `ignoreCount` is set to `true`, the count of the
436
+ * nodes in the binary tree will not be updated after removing a node. If `ignoreCount`
437
+ * @returns The function `remove` returns an array of `BinaryTreeDeletedResult<N>` objects.
438
+ */
439
+ remove(nodeOrId: N | BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeletedResult<N>[] {
440
+ const bstDeletedResult: BinaryTreeDeletedResult<N>[] = [];
441
+ if (!this.root) return bstDeletedResult;
442
+
443
+ const curr: N | null = (typeof nodeOrId === 'number') ? this.get(nodeOrId) : nodeOrId;
444
+ if (!curr) return bstDeletedResult;
445
+
446
+ const parent: N | null = curr?.parent ? curr.parent : null;
447
+ let needBalanced: N | null = null, orgCurrent = curr;
448
+
449
+ if (!curr.left) {
450
+ if (!parent) {
451
+ if (curr.right !== undefined) this._setRoot(curr.right);
452
+ } else {
453
+ const {familyPosition: fp} = curr;
454
+ if (fp === FamilyPosition.LEFT || fp === FamilyPosition.ROOT_LEFT) {
455
+ parent.left = curr.right;
456
+ } else if (fp === FamilyPosition.RIGHT || fp === FamilyPosition.ROOT_RIGHT) {
457
+ parent.right = curr.right;
458
+ }
459
+ needBalanced = parent;
460
+ }
461
+ } else {
462
+ const leftSubTreeRightMost = curr.left ? this.getRightMost(curr.left) : null;
463
+ if (leftSubTreeRightMost) {
464
+ const parentOfLeftSubTreeMax = leftSubTreeRightMost.parent;
465
+ orgCurrent = this.swapLocation(curr, leftSubTreeRightMost);
466
+ if (parentOfLeftSubTreeMax) {
467
+ if (parentOfLeftSubTreeMax.right === leftSubTreeRightMost) parentOfLeftSubTreeMax.right = leftSubTreeRightMost.left;
468
+ else parentOfLeftSubTreeMax.left = leftSubTreeRightMost.left;
469
+ needBalanced = parentOfLeftSubTreeMax;
470
+ }
471
+ }
472
+ }
473
+ this._setSize(this.size - 1);
474
+
475
+ bstDeletedResult.push({deleted: orgCurrent, needBalanced});
476
+ return bstDeletedResult;
477
+ }
478
+
479
+ /**
480
+ * The function calculates the depth of a node in a binary tree.
481
+ * @param {N | BinaryTreeNodeId | null} beginRoot - The `beginRoot` parameter can be one of the following:
482
+ * @returns the depth of the given node or binary tree.
483
+ */
484
+ getDepth(beginRoot: N | BinaryTreeNodeId | null): number {
485
+ if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
486
+
487
+ let depth = 0;
488
+ while (beginRoot?.parent) {
489
+ depth++;
490
+ beginRoot = beginRoot.parent;
491
+ }
492
+ return depth;
493
+ }
494
+
495
+ /**
496
+ * The `getHeight` function calculates the maximum height of a binary tree, either recursively or iteratively.
497
+ * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
498
+ * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
499
+ * node), or `null`.
500
+ * @returns the height of the binary tree.
501
+ */
502
+ getHeight(beginRoot?: N | BinaryTreeNodeId | null): number {
503
+ beginRoot = beginRoot ?? this.root;
504
+
505
+ if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
506
+ if (!beginRoot) return -1;
507
+
508
+ if (this._loopType === LoopType.RECURSIVE) {
509
+ const _getMaxHeight = (cur: N | null | undefined): number => {
510
+ if (!cur) return -1;
511
+ const leftHeight = _getMaxHeight(cur.left);
512
+ const rightHeight = _getMaxHeight(cur.right);
513
+ return Math.max(leftHeight, rightHeight) + 1;
514
+ };
515
+
516
+ return _getMaxHeight(beginRoot);
517
+ } else {
518
+ if (!beginRoot) {
519
+ return -1;
520
+ }
521
+
522
+ const stack: { node: N; depth: number }[] = [{node: beginRoot, depth: 0}];
523
+ let maxHeight = 0;
524
+
525
+ while (stack.length > 0) {
526
+ const {node, depth} = stack.pop()!;
527
+
528
+ if (node.left) {
529
+ stack.push({node: node.left, depth: depth + 1});
530
+ }
531
+
532
+ if (node.right) {
533
+ stack.push({node: node.right, depth: depth + 1});
534
+ }
535
+
536
+ maxHeight = Math.max(maxHeight, depth);
537
+ }
538
+
539
+ return maxHeight;
540
+ }
541
+ }
542
+
543
+ /**
544
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
545
+ * approach.
546
+ * @param {N | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type `N` or `null`. It
547
+ * represents the starting node from which to calculate the minimum height of a binary tree. If no value is provided
548
+ * for `beginRoot`, the `this.root` property is used as the default value.
549
+ * @returns The function `getMinHeight` returns the minimum height of the binary tree.
550
+ */
551
+ getMinHeight(beginRoot?: N | null): number {
552
+ beginRoot = beginRoot || this.root;
553
+ if (!beginRoot) return -1;
554
+
555
+ if (this._loopType === LoopType.RECURSIVE) {
556
+ const _getMinHeight = (cur: N | null | undefined): number => {
557
+ if (!cur) return 0;
558
+ if (!cur.left && !cur.right) return 0;
559
+ const leftMinHeight = _getMinHeight(cur.left);
560
+ const rightMinHeight = _getMinHeight(cur.right);
561
+ return Math.min(leftMinHeight, rightMinHeight) + 1;
562
+ };
563
+
564
+ return _getMinHeight(beginRoot);
565
+ } else {
566
+ const stack: N[] = [];
567
+ let node: N | null | undefined = beginRoot, last: N | null = null;
568
+ const depths: Map<N, number> = new Map();
569
+
570
+ while (stack.length > 0 || node) {
571
+ if (node) {
572
+ stack.push(node);
573
+ node = node.left;
574
+ } else {
575
+ node = stack[stack.length - 1]
576
+ if (!node.right || last === node.right) {
577
+ node = stack.pop();
578
+ if (node) {
579
+ const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
580
+ const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
581
+ depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
582
+ last = node;
583
+ node = null;
584
+ }
585
+ } else node = node.right
586
+ }
587
+ }
588
+
589
+ return depths.get(beginRoot) ?? -1;
590
+ }
591
+ }
592
+
593
+ /**
594
+ * The function checks if a binary tree is perfectly balanced by comparing the minimum height and the height of the
595
+ * tree.
596
+ * @param {N | null} [beginRoot] - The parameter `beginRoot` is of type `N` or `null`. It represents the root node of a
597
+ * tree or null if the tree is empty.
598
+ * @returns The method is returning a boolean value.
599
+ */
600
+ isPerfectlyBalanced(beginRoot?: N | null): boolean {
601
+ return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
602
+ }
603
+
604
+ /**
605
+ * The function `getNodes` returns an array of nodes that match a given property name and value in a binary tree.
606
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
607
+ * generic type `N`. It represents the property of the binary tree node that you want to search for.
608
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
609
+ * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
610
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
611
+ * return only one node that matches the given `nodeProperty` or `propertyName`. If `onlyOne` is set to `true`, the
612
+ * function will stop traversing the tree and return the first matching node. If `only
613
+ * @returns an array of nodes (type N).
614
+ */
615
+ getNodes(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean): N[] {
616
+ if (!this.root) return [];
617
+ propertyName = propertyName ?? 'id';
618
+
619
+ const result: N[] = [];
620
+
621
+ if (this.loopType === LoopType.RECURSIVE) {
622
+ const _traverse = (cur: N) => {
623
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return;
624
+ if (!cur.left && !cur.right) return;
625
+ cur.left && _traverse(cur.left);
626
+ cur.right && _traverse(cur.right);
627
+ }
628
+
629
+ _traverse(this.root);
630
+ } else {
631
+ const queue: N[] = [this.root];
632
+ while (queue.length > 0) {
633
+ const cur = queue.shift();
634
+ if (cur) {
635
+ if (this._pushByPropertyNameStopOrNot(cur, result, nodeProperty, propertyName, onlyOne)) return result;
636
+ cur.left && queue.push(cur.left);
637
+ cur.right && queue.push(cur.right);
638
+ }
639
+ }
640
+ }
641
+
642
+ return result;
643
+ }
644
+
645
+ /**
646
+ * The function checks if a binary tree node has a specific property.
647
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
648
+ * It represents the property of the binary tree node that you want to check.
649
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
650
+ * specifies the name of the property to be checked in the nodes. If not provided, it defaults to 'id'.
651
+ * @returns a boolean value.
652
+ */
653
+ has(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName): boolean {
654
+ propertyName = propertyName ?? 'id';
655
+ return this.getNodes(nodeProperty, propertyName).length > 0;
656
+ }
657
+
658
+ /**
659
+ * The function returns the first node that matches the given property name and value, or null if no matching node is
660
+ * found.
661
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or `N`.
662
+ * It represents the property of the binary tree node that you want to search for.
663
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
664
+ * specifies the property name to be used for searching the binary tree nodes. If this parameter is not provided, the
665
+ * default value is set to `'id'`.
666
+ * @returns either the value of the specified property of the node, or the node itself if no property name is provided.
667
+ * If no matching node is found, it returns null.
668
+ */
669
+ get(nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName): N | null {
670
+ propertyName = propertyName ?? 'id';
671
+ return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
672
+ }
673
+
674
+ /**
675
+ * The function getPathToRoot takes a node and returns an array of nodes representing the path from the given node to
676
+ * the root node.
677
+ * @param {N} node - The parameter `node` represents a node in a tree data structure.
678
+ * @returns The function `getPathToRoot` returns an array of nodes (`N[]`).
679
+ */
680
+ getPathToRoot(node: N): N[] {
681
+ const result: N[] = [];
682
+ while (node.parent) {
683
+ result.unshift(node);
684
+ node = node.parent;
685
+ }
686
+ result.unshift(node);
687
+ return result;
688
+ }
689
+
690
+ getLeftMost(): N | null;
691
+
692
+ getLeftMost(node: N): N;
693
+
694
+ /**
695
+ * The `getLeftMost` function returns the leftmost node in a binary tree, starting from a specified node or the root if
696
+ * no node is specified.
697
+ * @param {N | BinaryTreeNodeId | null} [beginRoot] - The `beginRoot` parameter is optional and can be of type `N` (a
698
+ * generic type representing a node in a binary tree), `BinaryTreeNodeId` (a type representing the ID of a binary tree
699
+ * node), or `null`.
700
+ * @returns The function `getLeftMost` returns the leftmost node in a binary tree. If the `beginRoot` parameter is
701
+ * provided, it starts the traversal from that node. If `beginRoot` is not provided or is `null`, it starts the
702
+ * traversal from the root of the binary tree. If there are no nodes in the binary tree, it returns `null`.
703
+ */
704
+ getLeftMost(beginRoot?: N | BinaryTreeNodeId | null): N | null {
705
+ if (typeof beginRoot === 'number') beginRoot = this.get(beginRoot, 'id');
706
+
707
+ beginRoot = beginRoot ?? this.root;
708
+ if (!beginRoot) return beginRoot;
709
+
710
+ if (this._loopType === LoopType.RECURSIVE) {
711
+
712
+ const _traverse = (cur: N): N => {
713
+ if (!cur.left) return cur;
714
+ return _traverse(cur.left);
715
+ }
716
+
717
+ return _traverse(beginRoot);
718
+ } else {
719
+ // Indirect implementation of iteration using tail recursion optimization
720
+ const _traverse = trampoline((cur: N) => {
721
+ if (!cur.left) return cur;
722
+ return _traverse.cont(cur.left);
723
+ });
724
+
725
+ return _traverse(beginRoot);
726
+ }
727
+ }
728
+
729
+ getRightMost(): N | null;
730
+
731
+ getRightMost(node: N): N;
732
+
733
+ /**
734
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
735
+ * tail recursion optimization.
736
+ * @param {N | null} [node] - The `node` parameter is an optional parameter of type `N` or `null`. It represents the
737
+ * starting node from which we want to find the rightmost node. If no node is provided, the `node` parameter defaults
738
+ * to `this.root`, which is the root node of the data structure
739
+ * @returns The function `getRightMost` returns the rightmost node (`N`) in a binary tree. If the `node` parameter is
740
+ * not provided, it defaults to the root node of the tree. If the tree is empty or the `node` parameter is `null`, the
741
+ * function returns `null`.
742
+ */
743
+ getRightMost(node?: N | null): N | null {
744
+ node = node ?? this.root;
745
+ if (!node) return node;
746
+
747
+ if (this._loopType === LoopType.RECURSIVE) {
748
+ const _traverse = (cur: N): N => {
749
+ if (!cur.right) return cur;
750
+ return _traverse(cur.right);
751
+ }
752
+
753
+ return _traverse(node);
754
+ } else {
755
+ // Indirect implementation of iteration using tail recursion optimization
756
+ const _traverse = trampoline((cur: N) => {
757
+ if (!cur.right) return cur;
758
+ return _traverse.cont(cur.right);
759
+ });
760
+
761
+ return _traverse(node);
762
+ }
763
+ }
764
+
765
+ /**
766
+ * The function checks if a binary search tree is valid by traversing it either recursively or iteratively.
767
+ * @param {N | null} node - The `node` parameter represents the root node of a binary search tree (BST).
768
+ * @returns a boolean value.
769
+ */
770
+ isBSTByRooted(node: N | null): boolean {
771
+ // TODO there is a bug
772
+ if (!node) return true;
773
+
774
+ if (this._loopType === LoopType.RECURSIVE) {
775
+ const dfs = (cur: N | null | undefined, min: BinaryTreeNodeId, max: BinaryTreeNodeId): boolean => {
776
+ if (!cur) return true;
777
+ if (cur.id <= min || cur.id >= max) return false;
778
+ return dfs(cur.left, min, cur.id) && dfs(cur.right, cur.id, max);
779
+ }
780
+
781
+ return dfs(node, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER);
782
+ } else {
783
+ const stack = [];
784
+ let prev = Number.MIN_SAFE_INTEGER, curr: N | null | undefined = node;
785
+ while (curr || stack.length > 0) {
786
+ while (curr) {
787
+ stack.push(curr);
788
+ curr = curr.left;
789
+ }
790
+ curr = stack.pop()!;
791
+ if (!(curr) || prev >= curr.id) return false;
792
+ prev = curr.id;
793
+ curr = curr.right;
794
+ }
795
+ return true;
796
+ }
797
+ }
798
+
799
+ /**
800
+ * The function checks if a binary tree is a binary search tree.
801
+ * @param {N | null} [node] - The `node` parameter is of type `N` or `null`. It represents the root node of a binary
802
+ * search tree (BST).
803
+ * @returns a boolean value.
804
+ */
805
+ isBST(node?: N | null): boolean {
806
+ return this.isBSTByRooted(this.root);
807
+ }
808
+
809
+ /**
810
+ * The function calculates the size of a subtree by traversing it either recursively or iteratively.
811
+ * @param {N | null | undefined} subTreeRoot - The `subTreeRoot` parameter represents the root node of a subtree in a
812
+ * binary tree.
813
+ * @returns the size of the subtree rooted at `subTreeRoot`.
814
+ */
815
+ getSubTreeSize(subTreeRoot: N | null | undefined) {
816
+ let size = 0;
817
+ if (!subTreeRoot) return size;
818
+
819
+ if (this._loopType === LoopType.RECURSIVE) {
820
+ const _traverse = (cur: N) => {
821
+ size++;
822
+ cur.left && _traverse(cur.left);
823
+ cur.right && _traverse(cur.right);
824
+ }
825
+
826
+ _traverse(subTreeRoot);
827
+ return size;
828
+ } else {
829
+ const stack: N[] = [subTreeRoot];
830
+
831
+ while (stack.length > 0) {
832
+ const cur = stack.pop()!;
833
+ size++;
834
+ cur.right && stack.push(cur.right);
835
+ cur.left && stack.push(cur.left);
836
+ }
837
+
838
+ return size;
839
+ }
840
+ }
841
+
842
+ /**
843
+ * The function `subTreeSum` calculates the sum of a specified property in a binary tree or subtree.
844
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
845
+ * tree or the ID of a binary tree node. It can also be `null` if there is no subtree.
846
+ * @param {BinaryTreeNodePropertyName} [propertyName] - propertyName is an optional parameter that specifies the
847
+ * property of the binary tree node to use for calculating the sum. It can be either 'id' or 'val'. If propertyName is
848
+ * not provided, it defaults to 'id'.
849
+ * @returns a number, which is the sum of the values of the specified property in the subtree rooted at `subTreeRoot`.
850
+ */
851
+ subTreeSum(subTreeRoot: N | BinaryTreeNodeId | null, propertyName ?: BinaryTreeNodePropertyName): number {
852
+ propertyName = propertyName ?? 'id';
853
+ if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
854
+
855
+ if (!subTreeRoot) return 0;
856
+
857
+ let sum = 0;
858
+
859
+ const _sumByProperty = (cur: N) => {
860
+ let needSum: number;
861
+ switch (propertyName) {
862
+ case 'id':
863
+ needSum = cur.id;
864
+ break;
865
+ case 'val':
866
+ needSum = typeof cur.val === 'number' ? cur.val : 0;
867
+ break;
868
+ default:
869
+ needSum = cur.id;
870
+ break;
871
+ }
872
+ return needSum;
873
+ }
874
+
875
+ if (this._loopType === LoopType.RECURSIVE) {
876
+ const _traverse = (cur: N): void => {
877
+ sum += _sumByProperty(cur);
878
+ cur.left && _traverse(cur.left);
879
+ cur.right && _traverse(cur.right);
880
+ }
881
+
882
+ _traverse(subTreeRoot);
883
+ } else {
884
+ const stack: N[] = [subTreeRoot];
885
+
886
+ while (stack.length > 0) {
887
+ const cur = stack.pop()!;
888
+ sum += _sumByProperty(cur);
889
+ cur.right && stack.push(cur.right);
890
+ cur.left && stack.push(cur.left);
891
+ }
892
+ }
893
+
894
+ return sum;
895
+ }
896
+
897
+ /**
898
+ * The function `subTreeAdd` adds a delta value to a specified property of each node in a subtree.
899
+ * @param {N | BinaryTreeNodeId | null} subTreeRoot - The `subTreeRoot` parameter represents the root node of a binary
900
+ * tree or the ID of a node in the binary tree. It can also be `null` if there is no subtree to add to.
901
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
902
+ * each node in the subtree should be incremented.
903
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
904
+ * specifies the property of the binary tree node that should be modified. If not provided, it defaults to 'id'.
905
+ * @returns a boolean value.
906
+ */
907
+ subTreeAdd(subTreeRoot: N | BinaryTreeNodeId | null, delta: number, propertyName ?: BinaryTreeNodePropertyName): boolean {
908
+ propertyName = propertyName ?? 'id';
909
+ if (typeof subTreeRoot === 'number') subTreeRoot = this.get(subTreeRoot, 'id');
910
+
911
+ if (!subTreeRoot) return false;
912
+
913
+ const _addByProperty = (cur: N) => {
914
+ switch (propertyName) {
915
+ case 'id':
916
+ cur.id += delta;
917
+ break;
918
+ default:
919
+ cur.id += delta;
920
+ break;
921
+ }
922
+ }
923
+
924
+ if (this._loopType === LoopType.RECURSIVE) {
925
+ const _traverse = (cur: N) => {
926
+ _addByProperty(cur);
927
+ cur.left && _traverse(cur.left);
928
+ cur.right && _traverse(cur.right);
929
+ };
930
+
931
+ _traverse(subTreeRoot);
932
+ } else {
933
+ const stack: N[] = [subTreeRoot];
934
+
935
+ while (stack.length > 0) {
936
+ const cur = stack.pop()!;
937
+
938
+ _addByProperty(cur);
939
+ cur.right && stack.push(cur.right);
940
+ cur.left && stack.push(cur.left);
941
+ }
942
+ }
943
+ return true;
944
+ }
945
+
946
+ BFS(): BinaryTreeNodeId[];
947
+
948
+ BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
949
+
950
+ BFS(nodeOrPropertyName: 'val'): N['val'][];
951
+
952
+ BFS(nodeOrPropertyName: 'node'): N[];
953
+
954
+ /**
955
+ * The BFS function performs a breadth-first search on a binary tree, accumulating properties of each node based on a
956
+ * specified property name.
957
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
958
+ * represents either a node or a property name. If a node is provided, the breadth-first search (BFS) algorithm will be
959
+ * performed starting from that node. If a property name is provided, the BFS algorithm will be performed starting from
960
+ * the
961
+ * @returns an instance of the `AbstractBinaryTreeNodeProperties` class with generic type `N`.
962
+ */
963
+ BFS(nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
964
+ nodeOrPropertyName = nodeOrPropertyName ?? 'id';
965
+ this._resetResults();
966
+ const queue: Array<N | null | undefined> = [this.root];
967
+
968
+ while (queue.length !== 0) {
969
+ const cur = queue.shift();
970
+ if (cur) {
971
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
972
+ if (cur?.left !== null) queue.push(cur.left);
973
+ if (cur?.right !== null) queue.push(cur.right);
974
+ }
975
+ }
976
+
977
+ return this._getResultByPropertyName(nodeOrPropertyName);
978
+ }
979
+
980
+ DFS(): BinaryTreeNodeId[];
981
+
982
+ DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
983
+
984
+ DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
985
+
986
+ DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
987
+
988
+ /**
989
+ * The DFS function performs a depth-first search traversal on a binary tree and returns the accumulated properties of
990
+ * each node based on the specified pattern and property name.
991
+ * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the traversal order of the
992
+ * binary tree. It can have three possible values:
993
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
994
+ * the name of a property of the nodes in the binary tree. This property will be used to accumulate values during the
995
+ * depth-first search traversal. If no `nodeOrPropertyName` is provided, the default value is `'id'`.
996
+ * @returns an instance of the AbstractBinaryTreeNodeProperties class, which contains the accumulated properties of the
997
+ * binary tree nodes based on the specified pattern and node or property name.
998
+ */
999
+ DFS(pattern ?: 'in' | 'pre' | 'post', nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1000
+ pattern = pattern ?? 'in';
1001
+ nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1002
+ this._resetResults();
1003
+ const _traverse = (node: N) => {
1004
+ switch (pattern) {
1005
+ case 'in':
1006
+ if (node.left) _traverse(node.left);
1007
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
1008
+ if (node.right) _traverse(node.right);
1009
+ break;
1010
+ case 'pre':
1011
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
1012
+ if (node.left) _traverse(node.left);
1013
+ if (node.right) _traverse(node.right);
1014
+ break;
1015
+ case 'post':
1016
+ if (node.left) _traverse(node.left);
1017
+ if (node.right) _traverse(node.right);
1018
+ this._accumulatedByPropertyName(node, nodeOrPropertyName);
1019
+ break;
1020
+ }
1021
+ };
1022
+
1023
+ this.root && _traverse(this.root);
1024
+ return this._getResultByPropertyName(nodeOrPropertyName);
1025
+ }
1026
+
1027
+
1028
+ // --- start additional methods ---
1029
+
1030
+ DFSIterative(): BinaryTreeNodeId[];
1031
+
1032
+ DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1033
+
1034
+ DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
1035
+
1036
+ DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
1037
+
1038
+ /**
1039
+ * The DFSIterative function performs an iterative depth-first search traversal on a binary tree, with the option to
1040
+ * specify the traversal pattern and the property name to accumulate results by.
1041
+ * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter determines the order in which the nodes of the
1042
+ * binary tree are visited during the depth-first search. It can have one of the following values:
1043
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
1044
+ * property of the nodes that you want to retrieve or perform operations on during the depth-first search traversal. By
1045
+ * default, it is set to `'id'`, which means that the traversal will accumulate results based on the `id` property of
1046
+ * the
1047
+ * @returns an object of type AbstractBinaryTreeNodeProperties<N>.
1048
+ */
1049
+ DFSIterative(pattern ?: 'in' | 'pre' | 'post', nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1050
+ pattern = pattern || 'in';
1051
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
1052
+ this._resetResults();
1053
+ if (!this.root) return this._getResultByPropertyName(nodeOrPropertyName);
1054
+ // 0: visit, 1: print
1055
+ const stack: { opt: 0 | 1, node: N | null | undefined }[] = [{opt: 0, node: this.root}];
1056
+
1057
+ while (stack.length > 0) {
1058
+ const cur = stack.pop();
1059
+ if (!cur || !cur.node) continue;
1060
+ if (cur.opt === 1) {
1061
+ this._accumulatedByPropertyName(cur.node, nodeOrPropertyName);
1062
+ } else {
1063
+ switch (pattern) {
1064
+ case 'in':
1065
+ stack.push({opt: 0, node: cur.node.right});
1066
+ stack.push({opt: 1, node: cur.node});
1067
+ stack.push({opt: 0, node: cur.node.left});
1068
+ break;
1069
+ case 'pre':
1070
+ stack.push({opt: 0, node: cur.node.right});
1071
+ stack.push({opt: 0, node: cur.node.left});
1072
+ stack.push({opt: 1, node: cur.node});
1073
+ break;
1074
+ case 'post':
1075
+ stack.push({opt: 1, node: cur.node});
1076
+ stack.push({opt: 0, node: cur.node.right});
1077
+ stack.push({opt: 0, node: cur.node.left});
1078
+ break;
1079
+ default:
1080
+ stack.push({opt: 0, node: cur.node.right});
1081
+ stack.push({opt: 1, node: cur.node});
1082
+ stack.push({opt: 0, node: cur.node.left});
1083
+ break;
1084
+ }
1085
+ }
1086
+ }
1087
+
1088
+ return this._getResultByPropertyName(nodeOrPropertyName);
1089
+ }
1090
+
1091
+ levelIterative(node: N | null): BinaryTreeNodeId[];
1092
+
1093
+ levelIterative(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1094
+
1095
+ levelIterative(node: N | null, nodeOrPropertyName?: 'val'): N['val'][];
1096
+
1097
+ levelIterative(node: N | null, nodeOrPropertyName?: 'node'): N[];
1098
+
1099
+ /**
1100
+ * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
1101
+ * in an array, based on a specified property name.
1102
+ * @param {N | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
1103
+ * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
1104
+ * the tree is used as the starting node.
1105
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1106
+ * can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
1107
+ * will accumulate results based on that property. If no property name is provided, the function will default to
1108
+ * accumulating results
1109
+ * @returns The function `levelIterative` returns an object of type `AbstractBinaryTreeNodeProperties<N>`.
1110
+ */
1111
+ levelIterative(node: N | null, nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1112
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
1113
+ node = node || this.root;
1114
+ if (!node) return [];
1115
+
1116
+ this._resetResults();
1117
+ const queue: N[] = [node];
1118
+
1119
+ while (queue.length > 0) {
1120
+ const cur = queue.shift();
1121
+ if (cur) {
1122
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1123
+ if (cur.left) {
1124
+ queue.push(cur.left);
1125
+ }
1126
+ if (cur.right) {
1127
+ queue.push(cur.right);
1128
+ }
1129
+ }
1130
+ }
1131
+
1132
+ return this._getResultByPropertyName(nodeOrPropertyName);
1133
+ }
1134
+
1135
+ listLevels(node: N | null): BinaryTreeNodeId[][];
1136
+
1137
+ listLevels(node: N | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[][];
1138
+
1139
+ listLevels(node: N | null, nodeOrPropertyName?: 'val'): N['val'][][];
1140
+
1141
+ listLevels(node: N | null, nodeOrPropertyName?: 'node'): N[][];
1142
+
1143
+ /**
1144
+ * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
1145
+ * @param {N | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the
1146
+ * root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
1147
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1148
+ * specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
1149
+ * values:
1150
+ * @returns The function `listLevels` returns a 2D array of `AbstractBinaryTreeNodeProperty<N>` objects.
1151
+ */
1152
+ listLevels(node: N | null, nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperty<N>[][] {
1153
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
1154
+ node = node || this.root;
1155
+ if (!node) return [];
1156
+
1157
+ const levelsNodes: AbstractBinaryTreeNodeProperty<N>[][] = [];
1158
+
1159
+ const collectByProperty = (node: N, level: number) => {
1160
+ switch (nodeOrPropertyName) {
1161
+ case 'id':
1162
+ levelsNodes[level].push(node.id);
1163
+ break;
1164
+ case 'val':
1165
+ levelsNodes[level].push(node.val);
1166
+ break;
1167
+ case 'node':
1168
+ levelsNodes[level].push(node);
1169
+ break;
1170
+ default:
1171
+ levelsNodes[level].push(node.id);
1172
+ break;
1173
+ }
1174
+ }
1175
+
1176
+ if (this.loopType === LoopType.RECURSIVE) {
1177
+ const _recursive = (node: N, level: number) => {
1178
+ if (!levelsNodes[level]) levelsNodes[level] = [];
1179
+ collectByProperty(node, level);
1180
+ if (node.left) _recursive(node.left, level + 1);
1181
+ if (node.right) _recursive(node.right, level + 1);
1182
+ };
1183
+
1184
+ _recursive(node, 0);
1185
+ } else {
1186
+ const stack: [N, number][] = [[node, 0]];
1187
+
1188
+ while (stack.length > 0) {
1189
+ const head = stack.pop()!;
1190
+ const [node, level] = head;
1191
+
1192
+ if (!levelsNodes[level]) levelsNodes[level] = [];
1193
+ collectByProperty(node, level);
1194
+ if (node.right) stack.push([node.right, level + 1]);
1195
+ if (node.left) stack.push([node.left, level + 1]);
1196
+ }
1197
+ }
1198
+
1199
+ return levelsNodes;
1200
+ }
1201
+
1202
+ /**
1203
+ * The function returns the predecessor of a given node in a binary tree.
1204
+ * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
1205
+ * @returns the predecessor of the given node in a binary tree.
1206
+ */
1207
+ getPredecessor(node: N): N {
1208
+ if (node.left) {
1209
+ let predecessor: N | null | undefined = node.left;
1210
+ while (!(predecessor) || predecessor.right && predecessor.right !== node) {
1211
+ if (predecessor) {
1212
+ predecessor = predecessor.right;
1213
+ }
1214
+ }
1215
+ return predecessor;
1216
+ } else {
1217
+ return node;
1218
+ }
1219
+ }
1220
+
1221
+ morris(): BinaryTreeNodeId[];
1222
+
1223
+ morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1224
+
1225
+ morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): N[];
1226
+
1227
+ /**
1228
+ * Time complexity is O(n)
1229
+ * Space complexity of Iterative DFS equals to recursive DFS which is O(n) because of the stack
1230
+ */
1231
+
1232
+ morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): N[];
1233
+
1234
+ /**
1235
+ * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
1236
+ * traversal algorithm.
1237
+ * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter determines the traversal pattern for the binary
1238
+ * tree. It can have one of three values:
1239
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
1240
+ * property name of the nodes that you want to retrieve. It can be any valid property name of the nodes in the binary
1241
+ * tree.
1242
+ * @returns an array of AbstractBinaryTreeNodeProperties<N> objects.
1243
+ */
1244
+ morris(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1245
+ if (this.root === null) return [];
1246
+
1247
+ pattern = pattern || 'in';
1248
+ nodeOrPropertyName = nodeOrPropertyName || 'id';
1249
+
1250
+ this._resetResults();
1251
+
1252
+ let cur: N | null | undefined = this.root;
1253
+ const _reverseEdge = (node: N | null | undefined) => {
1254
+ let pre: N | null | undefined = null;
1255
+ let next: N | null | undefined = null;
1256
+ while (node) {
1257
+ next = node.right;
1258
+ node.right = pre;
1259
+ pre = node;
1260
+ node = next;
1261
+ }
1262
+ return pre;
1263
+ };
1264
+ const _printEdge = (node: N | null) => {
1265
+ const tail: N | null | undefined = _reverseEdge(node);
1266
+ let cur: N | null | undefined = tail;
1267
+ while (cur) {
1268
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1269
+ cur = cur.right;
1270
+ }
1271
+ _reverseEdge(tail);
1272
+ };
1273
+ switch (pattern) {
1274
+ case 'in':
1275
+ while (cur) {
1276
+ if (cur.left) {
1277
+ const predecessor = this.getPredecessor(cur);
1278
+ if (!predecessor.right) {
1279
+ predecessor.right = cur;
1280
+ cur = cur.left;
1281
+ continue;
1282
+ } else {
1283
+ predecessor.right = null;
1284
+ }
1285
+ }
1286
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1287
+ cur = cur.right;
1288
+ }
1289
+ break;
1290
+ case 'pre':
1291
+ while (cur) {
1292
+ if (cur.left) {
1293
+ const predecessor = this.getPredecessor(cur);
1294
+ if (!predecessor.right) {
1295
+ predecessor.right = cur;
1296
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1297
+ cur = cur.left;
1298
+ continue;
1299
+ } else {
1300
+ predecessor.right = null;
1301
+ }
1302
+ } else {
1303
+ this._accumulatedByPropertyName(cur, nodeOrPropertyName);
1304
+ }
1305
+ cur = cur.right;
1306
+ }
1307
+ break;
1308
+ case 'post':
1309
+ while (cur) {
1310
+ if (cur.left) {
1311
+ const predecessor = this.getPredecessor(cur);
1312
+ if (predecessor.right === null) {
1313
+ predecessor.right = cur;
1314
+ cur = cur.left;
1315
+ continue;
1316
+ } else {
1317
+ predecessor.right = null;
1318
+ _printEdge(cur.left);
1319
+ }
1320
+ }
1321
+ cur = cur.right;
1322
+ }
1323
+ _printEdge(this.root);
1324
+ break;
1325
+ }
1326
+
1327
+ return this._getResultByPropertyName(nodeOrPropertyName);
1328
+ }
1329
+
1330
+ /**
1331
+ * The function sets the loop type for a protected variable.
1332
+ * @param {LoopType} value - The value parameter is of type LoopType.
1333
+ */
1334
+ protected _setLoopType(value: LoopType) {
1335
+ this._loopType = value;
1336
+ }
1337
+
1338
+ /**
1339
+ * The function sets the value of the `_visitedId` property in a protected manner.
1340
+ * @param {BinaryTreeNodeId[]} value - value is an array of BinaryTreeNodeId values.
1341
+ */
1342
+ protected _setVisitedId(value: BinaryTreeNodeId[]) {
1343
+ this._visitedId = value;
1344
+ }
1345
+
1346
+ /**
1347
+ * The function sets the value of the "_visitedVal" property to the given array.
1348
+ * @param value - An array of type N.
1349
+ */
1350
+ protected _setVisitedVal(value: Array<N>) {
1351
+ this._visitedVal = value;
1352
+ }
1353
+
1354
+ /**
1355
+ * The function sets the value of the _visitedNode property.
1356
+ * @param {N[]} value - N[] is an array of elements of type N.
1357
+ */
1358
+ protected _setVisitedNode(value: N[]) {
1359
+ this._visitedNode = value;
1360
+ }
1361
+
1362
+ /**
1363
+ * The function sets the value of the visitedCount property.
1364
+ * @param {number[]} value - The value parameter is an array of numbers.
1365
+ */
1366
+ protected setVisitedCount(value: number[]) {
1367
+ this._visitedCount = value;
1368
+ }
1369
+
1370
+ /**
1371
+ * The function sets the value of the `_visitedLeftSum` property to the provided array.
1372
+ * @param {number[]} value - An array of numbers that represents the visited left sum.
1373
+ */
1374
+ protected _setVisitedLeftSum(value: number[]) {
1375
+ this._visitedLeftSum = value;
1376
+ }
1377
+
1378
+ /**
1379
+ * The function sets the value of the _autoIncrementId property.
1380
+ * @param {boolean} value - The value parameter is a boolean that determines whether the id should be automatically
1381
+ * incremented or not. If value is true, the id will be automatically incremented. If value is false, the id will not
1382
+ * be automatically incremented.
1383
+ */
1384
+ protected _setAutoIncrementId(value: boolean) {
1385
+ this._autoIncrementId = value;
1386
+ }
1387
+
1388
+ /**
1389
+ * The function sets the maximum ID value.
1390
+ * @param {number} value - The value parameter is a number that represents the new maximum ID value.
1391
+ */
1392
+ protected _setMaxId(value: number) {
1393
+ this._maxId = value;
1394
+ }
1395
+
1396
+ /**
1397
+ * The function sets the value of a protected property called "_isMergeDuplicatedVal".
1398
+ * @param {boolean} value - The value parameter is a boolean value that determines whether the isMergeDuplicatedVal
1399
+ * property should be set to true or false.
1400
+ */
1401
+ protected _setIsDuplicatedVal(value: boolean) {
1402
+ this._isMergeDuplicatedVal = value;
1403
+ }
1404
+
1405
+ /**
1406
+ * The function sets the root property of an object to a given value, and if the value is not null, it also sets the
1407
+ * parent property of the value to undefined.
1408
+ * @param {N | null} v - The parameter `v` is of type `N | null`, which means it can either be of type `N` or `null`.
1409
+ */
1410
+ protected _setRoot(v: N | null) {
1411
+ if (v) {
1412
+ v.parent = undefined;
1413
+ }
1414
+ this._root = v;
1415
+ }
1416
+
1417
+ /**
1418
+ * The function sets the size of a protected variable.
1419
+ * @param {number} v - number
1420
+ */
1421
+ protected _setSize(v: number) {
1422
+ this._size = v;
1423
+ }
1424
+
1425
+ /**
1426
+ * The function `_resetResults` resets the values of several arrays used for tracking visited nodes and their
1427
+ * properties.
1428
+ */
1429
+ protected _resetResults() {
1430
+ this._visitedId = [];
1431
+ this._visitedVal = [];
1432
+ this._visitedNode = [];
1433
+ this._visitedLeftSum = [];
1434
+ }
1435
+
1436
+ /**
1437
+ * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1438
+ * a result array.
1439
+ * @param {N} cur - The current node being processed.
1440
+ * @param {(N | null | undefined)[]} result - An array that stores the matching nodes.
1441
+ * @param {BinaryTreeNodeId | N} nodeProperty - The `nodeProperty` parameter is either a `BinaryTreeNodeId` or a `N`
1442
+ * type. It represents the property value that we are comparing against in the switch statement.
1443
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1444
+ * specifies the property name to compare against when pushing nodes into the `result` array. It can be either `'id'`
1445
+ * or `'val'`. If it is not provided or is not equal to `'id'` or `'val'`, the
1446
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1447
+ * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1448
+ * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1449
+ * @returns a boolean value indicating whether only one matching node should be pushed into the result array.
1450
+ */
1451
+ protected _pushByPropertyNameStopOrNot(cur: N, result: (N | null | undefined)[], nodeProperty: BinaryTreeNodeId | N, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
1452
+ switch (propertyName) {
1453
+ case 'id':
1454
+ if (cur.id === nodeProperty) {
1455
+ result.push(cur);
1456
+ return !!onlyOne;
1457
+ }
1458
+ break;
1459
+ case 'val':
1460
+ if (cur.val === nodeProperty) {
1461
+ result.push(cur);
1462
+ return !!onlyOne;
1463
+ }
1464
+ break;
1465
+ default:
1466
+ if (cur.id === nodeProperty) {
1467
+ result.push(cur);
1468
+ return !!onlyOne;
1469
+ }
1470
+ break;
1471
+ }
1472
+ }
1473
+
1474
+ /**
1475
+ * The function `_accumulatedByPropertyName` accumulates values from a given node based on the specified property name.
1476
+ * @param {N} node - The `node` parameter is of type `N`, which represents a node in a data structure.
1477
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1478
+ * can be either a string representing a property name or a reference to a `Node` object. If it is a string, it
1479
+ * specifies the property name to be used for accumulating values. If it is a `Node` object, it specifies
1480
+ */
1481
+ protected _accumulatedByPropertyName(node: N, nodeOrPropertyName ?: NodeOrPropertyName) {
1482
+ nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1483
+
1484
+ switch (nodeOrPropertyName) {
1485
+ case 'id':
1486
+ this._visitedId.push(node.id);
1487
+ break;
1488
+ case 'val':
1489
+ this._visitedVal.push(node.val);
1490
+ break;
1491
+ case 'node':
1492
+ this._visitedNode.push(node);
1493
+ break;
1494
+ default:
1495
+ this._visitedId.push(node.id);
1496
+ break;
1497
+ }
1498
+ }
1499
+
1500
+ /**
1501
+ * The time complexity of Morris traversal is O(n), it's may slower than others
1502
+ * The space complexity Morris traversal is O(1) because no using stack
1503
+ */
1504
+
1505
+ /**
1506
+ * The function `_getResultByPropertyName` returns the corresponding property value based on the given node or property
1507
+ * name.
1508
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1509
+ * can accept either a `NodeOrPropertyName` type or be undefined.
1510
+ * @returns The method `_getResultByPropertyName` returns an instance of `AbstractBinaryTreeNodeProperties<N>`.
1511
+ */
1512
+ protected _getResultByPropertyName(nodeOrPropertyName ?: NodeOrPropertyName): AbstractBinaryTreeNodeProperties<N> {
1513
+ nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1514
+
1515
+ switch (nodeOrPropertyName) {
1516
+ case 'id':
1517
+ return this._visitedId;
1518
+ case 'val':
1519
+ return this._visitedVal;
1520
+ case 'node':
1521
+ return this._visitedNode;
1522
+ default:
1523
+ return this._visitedId;
1524
+ }
1525
+ }
1526
+
1527
+ // --- end additional methods ---
1528
+ }