data-structure-typed 0.9.16 → 1.3.0

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