data-structure-typed 0.8.18 → 1.12.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (287) hide show
  1. package/.dependency-cruiser.js +449 -0
  2. package/.idea/data-structure-typed.iml +2 -0
  3. package/.idea/modules.xml +1 -1
  4. package/README.md +298 -2
  5. package/dist/data-structures/binary-tree/aa-tree.js +5 -2
  6. package/dist/data-structures/binary-tree/avl-tree.d.ts +58 -5
  7. package/dist/data-structures/binary-tree/avl-tree.js +150 -46
  8. package/dist/data-structures/binary-tree/b-tree.js +5 -2
  9. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +28 -1
  10. package/dist/data-structures/binary-tree/binary-indexed-tree.js +41 -13
  11. package/dist/data-structures/binary-tree/binary-tree.d.ts +230 -36
  12. package/dist/data-structures/binary-tree/binary-tree.js +747 -369
  13. package/dist/data-structures/binary-tree/bst.d.ts +20 -8
  14. package/dist/data-structures/binary-tree/bst.js +164 -107
  15. package/dist/data-structures/binary-tree/rb-tree.js +5 -2
  16. package/dist/data-structures/binary-tree/segment-tree.d.ts +7 -3
  17. package/dist/data-structures/binary-tree/segment-tree.js +95 -61
  18. package/dist/data-structures/binary-tree/splay-tree.js +5 -2
  19. package/dist/data-structures/binary-tree/tree-multiset.d.ts +5 -5
  20. package/dist/data-structures/binary-tree/tree-multiset.js +35 -11
  21. package/dist/data-structures/binary-tree/two-three-tree.js +5 -2
  22. package/dist/data-structures/graph/abstract-graph.d.ts +168 -46
  23. package/dist/data-structures/graph/abstract-graph.js +712 -323
  24. package/dist/data-structures/graph/directed-graph.d.ts +114 -12
  25. package/dist/data-structures/graph/directed-graph.js +372 -128
  26. package/dist/data-structures/graph/undirected-graph.d.ts +67 -3
  27. package/dist/data-structures/graph/undirected-graph.js +233 -81
  28. package/dist/data-structures/hash/coordinate-map.d.ts +33 -1
  29. package/dist/data-structures/hash/coordinate-map.js +70 -20
  30. package/dist/data-structures/hash/coordinate-set.d.ts +25 -0
  31. package/dist/data-structures/hash/coordinate-set.js +58 -15
  32. package/dist/data-structures/hash/index.d.ts +5 -0
  33. package/dist/data-structures/hash/index.js +5 -0
  34. package/dist/data-structures/heap/heap.d.ts +26 -37
  35. package/dist/data-structures/heap/heap.js +56 -60
  36. package/dist/data-structures/heap/max-heap.d.ts +8 -2
  37. package/dist/data-structures/heap/max-heap.js +32 -9
  38. package/dist/data-structures/heap/min-heap.d.ts +9 -2
  39. package/dist/data-structures/heap/min-heap.js +33 -9
  40. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +14 -7
  41. package/dist/data-structures/linked-list/doubly-linked-list.js +101 -61
  42. package/dist/data-structures/linked-list/singly-linked-list.d.ts +18 -19
  43. package/dist/data-structures/linked-list/singly-linked-list.js +312 -174
  44. package/dist/data-structures/matrix/matrix.d.ts +9 -0
  45. package/dist/data-structures/matrix/matrix.js +19 -7
  46. package/dist/data-structures/matrix/matrix2d.d.ts +84 -4
  47. package/dist/data-structures/matrix/matrix2d.js +158 -61
  48. package/dist/data-structures/matrix/navigator.d.ts +34 -16
  49. package/dist/data-structures/matrix/navigator.js +65 -18
  50. package/dist/data-structures/matrix/vector2d.d.ts +153 -29
  51. package/dist/data-structures/matrix/vector2d.js +249 -102
  52. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +11 -2
  53. package/dist/data-structures/priority-queue/max-priority-queue.js +33 -8
  54. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +11 -2
  55. package/dist/data-structures/priority-queue/min-priority-queue.js +33 -8
  56. package/dist/data-structures/priority-queue/priority-queue.d.ts +145 -21
  57. package/dist/data-structures/priority-queue/priority-queue.js +285 -116
  58. package/dist/data-structures/queue/deque.d.ts +69 -0
  59. package/dist/data-structures/queue/deque.js +151 -56
  60. package/dist/data-structures/queue/queue.d.ts +34 -37
  61. package/dist/data-structures/queue/queue.js +59 -61
  62. package/dist/data-structures/stack/stack.d.ts +29 -35
  63. package/dist/data-structures/stack/stack.js +51 -56
  64. package/dist/data-structures/trie/trie.d.ts +36 -6
  65. package/dist/data-structures/trie/trie.js +256 -83
  66. package/dist/data-structures/types/abstract-graph.d.ts +29 -0
  67. package/dist/data-structures/types/abstract-graph.js +2 -0
  68. package/dist/data-structures/types/avl-tree.d.ts +5 -0
  69. package/dist/data-structures/types/avl-tree.js +2 -0
  70. package/dist/data-structures/types/binary-tree.d.ts +16 -0
  71. package/dist/data-structures/types/binary-tree.js +2 -0
  72. package/dist/data-structures/types/bst.d.ts +7 -0
  73. package/dist/data-structures/types/bst.js +2 -0
  74. package/dist/data-structures/types/directed-graph.d.ts +10 -0
  75. package/dist/data-structures/types/directed-graph.js +2 -0
  76. package/dist/data-structures/types/doubly-linked-list.d.ts +1 -0
  77. package/dist/data-structures/types/doubly-linked-list.js +2 -0
  78. package/dist/data-structures/types/heap.d.ts +7 -0
  79. package/dist/data-structures/types/heap.js +2 -0
  80. package/dist/data-structures/types/index.d.ts +13 -0
  81. package/dist/data-structures/types/index.js +29 -0
  82. package/dist/data-structures/types/navigator.d.ts +14 -0
  83. package/dist/data-structures/types/navigator.js +2 -0
  84. package/dist/data-structures/types/priority-queue.d.ts +7 -0
  85. package/dist/data-structures/types/priority-queue.js +2 -0
  86. package/dist/data-structures/types/segment-tree.d.ts +1 -0
  87. package/dist/data-structures/types/segment-tree.js +2 -0
  88. package/dist/data-structures/types/singly-linked-list.js +2 -0
  89. package/dist/data-structures/types/tree-multiset.d.ts +5 -0
  90. package/dist/data-structures/types/tree-multiset.js +2 -0
  91. package/dist/utils/trampoline.d.ts +14 -0
  92. package/dist/utils/trampoline.js +130 -0
  93. package/dist/utils/types/index.js +17 -0
  94. package/dist/{types → utils}/types/utils.d.ts +15 -1
  95. package/dist/{types → utils/types}/utils.js +21 -19
  96. package/dist/{utils.d.ts → utils/utils.d.ts} +5 -22
  97. package/dist/utils/utils.js +651 -0
  98. package/docs/.nojekyll +1 -0
  99. package/docs/assets/highlight.css +85 -0
  100. package/docs/assets/main.js +58 -0
  101. package/docs/assets/search.js +1 -0
  102. package/docs/assets/style.css +1367 -0
  103. package/docs/classes/AVLTree.html +2046 -0
  104. package/docs/classes/AVLTreeNode.html +423 -0
  105. package/docs/classes/AaTree.html +117 -0
  106. package/docs/classes/AbstractEdge.html +198 -0
  107. package/docs/classes/AbstractGraph.html +891 -0
  108. package/docs/classes/AbstractVertex.html +164 -0
  109. package/docs/classes/ArrayDeque.html +384 -0
  110. package/docs/classes/BST.html +1893 -0
  111. package/docs/classes/BSTNode.html +425 -0
  112. package/docs/classes/BTree.html +117 -0
  113. package/docs/classes/BinaryIndexedTree.html +244 -0
  114. package/docs/classes/BinaryTree.html +1754 -0
  115. package/docs/classes/BinaryTreeNode.html +396 -0
  116. package/docs/classes/Character.html +165 -0
  117. package/docs/classes/CoordinateMap.html +394 -0
  118. package/docs/classes/CoordinateSet.html +355 -0
  119. package/docs/classes/Deque.html +617 -0
  120. package/docs/classes/DirectedEdge.html +247 -0
  121. package/docs/classes/DirectedGraph.html +1207 -0
  122. package/docs/classes/DirectedVertex.html +154 -0
  123. package/docs/classes/DoublyLinkedList.html +619 -0
  124. package/docs/classes/DoublyLinkedListNode.html +160 -0
  125. package/docs/classes/Heap.html +315 -0
  126. package/docs/classes/Matrix2D.html +447 -0
  127. package/docs/classes/MatrixNTI2D.html +181 -0
  128. package/docs/classes/MaxHeap.html +325 -0
  129. package/docs/classes/MaxPriorityQueue.html +668 -0
  130. package/docs/classes/MinHeap.html +326 -0
  131. package/docs/classes/MinPriorityQueue.html +668 -0
  132. package/docs/classes/Navigator.html +285 -0
  133. package/docs/classes/ObjectDeque.html +289 -0
  134. package/docs/classes/PriorityQueue.html +643 -0
  135. package/docs/classes/Queue.html +337 -0
  136. package/docs/classes/RBTree.html +117 -0
  137. package/docs/classes/SegmentTree.html +234 -0
  138. package/docs/classes/SegmentTreeNode.html +302 -0
  139. package/docs/classes/SinglyLinkedList.html +1035 -0
  140. package/docs/classes/SinglyLinkedListNode.html +304 -0
  141. package/docs/classes/SplayTree.html +117 -0
  142. package/docs/classes/Stack.html +313 -0
  143. package/docs/classes/TreeMultiSet.html +1897 -0
  144. package/docs/classes/Trie.html +317 -0
  145. package/docs/classes/TrieNode.html +221 -0
  146. package/docs/classes/TwoThreeTree.html +117 -0
  147. package/docs/classes/UndirectedEdge.html +220 -0
  148. package/docs/classes/UndirectedGraph.html +1006 -0
  149. package/docs/classes/UndirectedVertex.html +154 -0
  150. package/docs/classes/Vector2D.html +746 -0
  151. package/docs/enums/CP.html +126 -0
  152. package/docs/enums/FamilyPosition.html +126 -0
  153. package/docs/enums/LoopType.html +119 -0
  154. package/docs/index.html +288 -0
  155. package/docs/modules.html +146 -0
  156. package/jest.config.js +5 -0
  157. package/package.json +33 -47
  158. package/rename_clear_files.sh +29 -0
  159. package/src/assets/complexities-diff.jpg +0 -0
  160. package/src/assets/data-structure-complexities.jpg +0 -0
  161. package/src/data-structures/binary-tree/avl-tree.ts +58 -6
  162. package/src/data-structures/binary-tree/binary-indexed-tree.ts +31 -4
  163. package/src/data-structures/binary-tree/binary-tree.ts +460 -145
  164. package/src/data-structures/binary-tree/bst.ts +31 -25
  165. package/src/data-structures/binary-tree/diagrams/avl-tree-inserting.gif +0 -0
  166. package/src/data-structures/binary-tree/diagrams/bst-rotation.gif +0 -0
  167. package/src/data-structures/binary-tree/diagrams/segment-tree.png +0 -0
  168. package/src/data-structures/binary-tree/segment-tree.ts +25 -12
  169. package/src/data-structures/binary-tree/tree-multiset.ts +5 -4
  170. package/src/data-structures/diagrams/README.md +5 -0
  171. package/src/data-structures/graph/abstract-graph.ts +224 -108
  172. package/src/data-structures/graph/diagrams/adjacency-list-pros-cons.jpg +0 -0
  173. package/src/data-structures/graph/diagrams/adjacency-list.jpg +0 -0
  174. package/src/data-structures/graph/diagrams/adjacency-matrix-pros-cons.jpg +0 -0
  175. package/src/data-structures/graph/diagrams/adjacency-matrix.jpg +0 -0
  176. package/src/data-structures/graph/diagrams/dfs-can-do.jpg +0 -0
  177. package/src/data-structures/graph/diagrams/edge-list-pros-cons.jpg +0 -0
  178. package/src/data-structures/graph/diagrams/edge-list.jpg +0 -0
  179. package/src/data-structures/graph/diagrams/max-flow.jpg +0 -0
  180. package/src/data-structures/graph/diagrams/mst.jpg +0 -0
  181. package/src/data-structures/graph/diagrams/tarjan-articulation-point-bridge.png +0 -0
  182. package/src/data-structures/graph/diagrams/tarjan-complicate-simple.png +0 -0
  183. package/src/data-structures/graph/diagrams/tarjan-strongly-connected-component.png +0 -0
  184. package/src/data-structures/graph/diagrams/tarjan.mp4 +0 -0
  185. package/src/data-structures/graph/diagrams/tarjan.webp +0 -0
  186. package/src/data-structures/graph/directed-graph.ts +132 -26
  187. package/src/data-structures/graph/undirected-graph.ts +78 -11
  188. package/src/data-structures/hash/coordinate-map.ts +33 -1
  189. package/src/data-structures/hash/coordinate-set.ts +25 -0
  190. package/src/data-structures/hash/index.ts +5 -0
  191. package/src/data-structures/heap/heap.ts +27 -41
  192. package/src/data-structures/heap/max-heap.ts +8 -2
  193. package/src/data-structures/heap/min-heap.ts +9 -2
  194. package/src/data-structures/linked-list/doubly-linked-list.ts +50 -17
  195. package/src/data-structures/linked-list/singly-linked-list.ts +56 -39
  196. package/src/data-structures/matrix/matrix.ts +11 -0
  197. package/src/data-structures/matrix/matrix2d.ts +90 -10
  198. package/src/data-structures/matrix/navigator.ts +34 -14
  199. package/src/data-structures/matrix/vector2d.ts +187 -63
  200. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -3
  201. package/src/data-structures/priority-queue/min-priority-queue.ts +12 -3
  202. package/src/data-structures/priority-queue/priority-queue.ts +200 -78
  203. package/src/data-structures/queue/deque.ts +71 -2
  204. package/src/data-structures/queue/queue.ts +37 -40
  205. package/src/data-structures/stack/stack.ts +32 -38
  206. package/src/data-structures/trie/trie.ts +83 -14
  207. package/src/data-structures/types/abstract-graph.ts +51 -0
  208. package/src/data-structures/types/avl-tree.ts +6 -0
  209. package/src/data-structures/types/binary-tree.ts +15 -0
  210. package/src/data-structures/types/bst.ts +5 -0
  211. package/src/data-structures/types/directed-graph.ts +18 -0
  212. package/src/data-structures/types/doubly-linked-list.ts +1 -0
  213. package/src/data-structures/types/heap.ts +8 -0
  214. package/src/data-structures/types/index.ts +13 -0
  215. package/src/data-structures/types/navigator.ts +13 -0
  216. package/src/data-structures/types/priority-queue.ts +9 -0
  217. package/src/data-structures/types/segment-tree.ts +1 -0
  218. package/src/data-structures/types/singly-linked-list.ts +1 -0
  219. package/src/data-structures/types/tree-multiset.ts +3 -0
  220. package/src/utils/index.ts +1 -0
  221. package/src/utils/trampoline.ts +51 -0
  222. package/src/utils/types/index.ts +1 -0
  223. package/src/{types → utils/types}/utils.ts +27 -5
  224. package/src/{utils.ts → utils/utils.ts} +41 -131
  225. package/tests/unit/data-structures/binary-tree/bst.test.ts +185 -0
  226. package/tests/unit/data-structures/graph/directed-graph.test.ts +71 -0
  227. package/{dist/types/data-structures/graph/index.d.ts → tests/unit/data-structures/graph/index.ts} +1 -1
  228. package/tests/unit/data-structures/graph/undirected-graph.ts +0 -0
  229. package/tsconfig.json +9 -6
  230. package/dist/data-structures/trampoline.d.ts +0 -25
  231. package/dist/data-structures/trampoline.js +0 -52
  232. package/dist/types/data-structures/binary-tree/aa-tree.d.ts +0 -2
  233. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +0 -21
  234. package/dist/types/data-structures/binary-tree/b-tree.d.ts +0 -2
  235. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -8
  236. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +0 -140
  237. package/dist/types/data-structures/binary-tree/bst.d.ts +0 -32
  238. package/dist/types/data-structures/binary-tree/index.d.ts +0 -4
  239. package/dist/types/data-structures/binary-tree/rb-tree.d.ts +0 -2
  240. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +0 -33
  241. package/dist/types/data-structures/binary-tree/splay-tree.d.ts +0 -2
  242. package/dist/types/data-structures/binary-tree/tree-multiset.d.ts +0 -11
  243. package/dist/types/data-structures/binary-tree/two-three-tree.d.ts +0 -2
  244. package/dist/types/data-structures/graph/abstract-graph.d.ts +0 -126
  245. package/dist/types/data-structures/graph/directed-graph.d.ts +0 -51
  246. package/dist/types/data-structures/graph/undirected-graph.d.ts +0 -24
  247. package/dist/types/data-structures/hash/coordinate-map.d.ts +0 -8
  248. package/dist/types/data-structures/hash/coordinate-set.d.ts +0 -7
  249. package/dist/types/data-structures/hash/index.d.ts +0 -1
  250. package/dist/types/data-structures/hash/pair.d.ts +0 -1
  251. package/dist/types/data-structures/hash/tree-map.d.ts +0 -1
  252. package/dist/types/data-structures/hash/tree-set.d.ts +0 -1
  253. package/dist/types/data-structures/heap/heap.d.ts +0 -72
  254. package/dist/types/data-structures/heap/index.d.ts +0 -3
  255. package/dist/types/data-structures/heap/max-heap.d.ts +0 -14
  256. package/dist/types/data-structures/heap/min-heap.d.ts +0 -14
  257. package/dist/types/data-structures/index.d.ts +0 -9
  258. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +0 -59
  259. package/dist/types/data-structures/linked-list/index.d.ts +0 -2
  260. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +0 -358
  261. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +0 -1
  262. package/dist/types/data-structures/matrix/index.d.ts +0 -3
  263. package/dist/types/data-structures/matrix/matrix.d.ts +0 -9
  264. package/dist/types/data-structures/matrix/matrix2d.d.ts +0 -25
  265. package/dist/types/data-structures/matrix/navigator.d.ts +0 -31
  266. package/dist/types/data-structures/matrix/vector2d.d.ts +0 -74
  267. package/dist/types/data-structures/priority-queue/index.d.ts +0 -3
  268. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +0 -4
  269. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +0 -4
  270. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +0 -36
  271. package/dist/types/data-structures/queue/deque.d.ts +0 -37
  272. package/dist/types/data-structures/queue/index.d.ts +0 -1
  273. package/dist/types/data-structures/queue/queue.d.ts +0 -76
  274. package/dist/types/data-structures/stack/index.d.ts +0 -1
  275. package/dist/types/data-structures/stack/stack.d.ts +0 -69
  276. package/dist/types/data-structures/trampoline.d.ts +0 -25
  277. package/dist/types/data-structures/trie/index.d.ts +0 -1
  278. package/dist/types/data-structures/trie/trie.d.ts +0 -28
  279. package/dist/types/utils.d.ts +0 -46
  280. package/dist/utils.js +0 -569
  281. package/src/data-structures/trampoline.ts +0 -91
  282. package/src/types/index.ts +0 -1
  283. /package/dist/{types/data-structures/hash/hash-table.d.ts → data-structures/types/singly-linked-list.d.ts} +0 -0
  284. /package/dist/{types → utils}/index.d.ts +0 -0
  285. /package/dist/{types → utils}/index.js +0 -0
  286. /package/dist/{types → utils}/types/index.d.ts +0 -0
  287. /package/{src/types/patches/index.d.ts → tests/unit/data-structures/graph/abstract-graph.ts} +0 -0
@@ -1,25 +1,32 @@
1
- import {ThunkOrValue, trampoline} from '../trampoline';
2
-
3
- export type BinaryTreeNodePropertyName = 'id' | 'val' | 'count';
4
- export type NodeOrPropertyName = 'node' | BinaryTreeNodePropertyName;
5
- export type DFSOrderPattern = 'in' | 'pre' | 'post';
6
- export type BinaryTreeNodeId = number;
7
- export type BinaryTreeDeleted<T> = { deleted: BinaryTreeNode<T> | null | undefined, needBalanced: BinaryTreeNode<T> | null };
8
- export type ResultByProperty<T> = T | BinaryTreeNode<T> | number | BinaryTreeNodeId;
9
- export type ResultsByProperty<T> = ResultByProperty<T>[];
10
-
11
- export interface BinaryTreeNodeObj<T> {
12
- id: BinaryTreeNodeId;
13
- val: T;
14
- count?: number;
15
- }
1
+ /**
2
+ * @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
3
+ * @license MIT
4
+ */
5
+
6
+ import {trampoline} from '../../utils/trampoline';
7
+ import type {
8
+ BinaryTreeDeleted,
9
+ BinaryTreeNodeId,
10
+ BinaryTreeNodePropertyName,
11
+ DFSOrderPattern,
12
+ NodeOrPropertyName,
13
+ ResultByProperty,
14
+ ResultsByProperty
15
+ } from '../types';
16
16
 
17
17
  export enum FamilyPosition {root, left, right}
18
18
 
19
19
  export enum LoopType { iterative = 1, recursive = 2}
20
20
 
21
21
  export class BinaryTreeNode<T> {
22
+ constructor(id: BinaryTreeNodeId, val: T, count?: number) {
23
+ this._id = id;
24
+ this._val = val;
25
+ this._count = count ?? 1;
26
+ }
27
+
22
28
  protected _id: BinaryTreeNodeId;
29
+
23
30
  get id(): BinaryTreeNodeId {
24
31
  return this._id;
25
32
  }
@@ -29,6 +36,7 @@ export class BinaryTreeNode<T> {
29
36
  }
30
37
 
31
38
  protected _val: T;
39
+
32
40
  get val(): T {
33
41
  return this._val;
34
42
  }
@@ -38,6 +46,7 @@ export class BinaryTreeNode<T> {
38
46
  }
39
47
 
40
48
  protected _left?: BinaryTreeNode<T> | null;
49
+
41
50
  get left(): BinaryTreeNode<T> | null | undefined {
42
51
  return this._left;
43
52
  }
@@ -51,6 +60,7 @@ export class BinaryTreeNode<T> {
51
60
  }
52
61
 
53
62
  protected _right?: BinaryTreeNode<T> | null;
63
+
54
64
  get right(): BinaryTreeNode<T> | null | undefined {
55
65
  return this._right;
56
66
  }
@@ -63,7 +73,8 @@ export class BinaryTreeNode<T> {
63
73
  this._right = v;
64
74
  }
65
75
 
66
- protected _parent: BinaryTreeNode<T> | null | undefined = undefined;
76
+ protected _parent: BinaryTreeNode<T> | null | undefined;
77
+
67
78
  get parent(): BinaryTreeNode<T> | null | undefined {
68
79
  return this._parent;
69
80
  }
@@ -73,6 +84,7 @@ export class BinaryTreeNode<T> {
73
84
  }
74
85
 
75
86
  protected _familyPosition: FamilyPosition = FamilyPosition.root;
87
+
76
88
  get familyPosition(): FamilyPosition {
77
89
  return this._familyPosition;
78
90
  }
@@ -82,6 +94,7 @@ export class BinaryTreeNode<T> {
82
94
  }
83
95
 
84
96
  protected _count = 1;
97
+
85
98
  get count(): number {
86
99
  return this._count;
87
100
  }
@@ -100,12 +113,6 @@ export class BinaryTreeNode<T> {
100
113
  this._height = v;
101
114
  }
102
115
 
103
- constructor(id: BinaryTreeNodeId, val: T, count?: number) {
104
- this._id = id;
105
- this._val = val;
106
- this._count = count ?? 1;
107
- }
108
-
109
116
  swapLocation(swapNode: BinaryTreeNode<T>): BinaryTreeNode<T> {
110
117
  const {val, count, height} = swapNode;
111
118
  const tempNode = new BinaryTreeNode<T>(swapNode.id, val);
@@ -131,8 +138,41 @@ export class BinaryTreeNode<T> {
131
138
  }
132
139
 
133
140
  export class BinaryTree<T> {
141
+ protected _loopType: LoopType = LoopType.iterative;
142
+ protected _visitedId: BinaryTreeNodeId[] = [];
143
+ protected _visitedVal: Array<T> = [];
144
+ protected _visitedNode: BinaryTreeNode<T>[] = [];
145
+ protected _visitedCount: number[] = [];
146
+ protected _visitedLeftSum: number[] = [];
147
+ private readonly _autoIncrementId: boolean = false;
148
+ private _maxId: number = -1;
149
+ private readonly _isDuplicatedVal: boolean = false;
150
+
151
+ /**
152
+ * The constructor function accepts an optional options object and sets the values of loopType, autoIncrementId, and
153
+ * isDuplicatedVal based on the provided options.
154
+ * @param [options] - An optional object that can contain the following properties:
155
+ */
156
+ constructor(options?: {
157
+ loopType?: LoopType,
158
+ autoIncrementId?: boolean,
159
+ isDuplicatedVal?: boolean
160
+ }) {
161
+ if (options !== undefined) {
162
+ const {
163
+ loopType = LoopType.iterative,
164
+ autoIncrementId = false,
165
+ isDuplicatedVal = false
166
+ } = options;
167
+ this._isDuplicatedVal = isDuplicatedVal;
168
+ this._autoIncrementId = autoIncrementId;
169
+ this._loopType = loopType;
170
+ }
171
+ }
172
+
134
173
  protected _root: BinaryTreeNode<T> | null = null;
135
- public get root(): BinaryTreeNode<T> | null {
174
+
175
+ get root(): BinaryTreeNode<T> | null {
136
176
  return this._root;
137
177
  }
138
178
 
@@ -145,6 +185,7 @@ export class BinaryTree<T> {
145
185
  }
146
186
 
147
187
  protected _size = 0;
188
+
148
189
  get size(): number {
149
190
  return this._size;
150
191
  }
@@ -154,6 +195,7 @@ export class BinaryTree<T> {
154
195
  }
155
196
 
156
197
  protected _count = 0;
198
+
157
199
  get count(): number {
158
200
  return this._count;
159
201
  }
@@ -162,46 +204,25 @@ export class BinaryTree<T> {
162
204
  this._count = v;
163
205
  }
164
206
 
165
- private readonly _autoIncrementId: boolean = false;
166
- private _maxId: number = -1;
167
- private readonly _isDuplicatedVal: boolean = false;
168
-
169
- protected _loopType: LoopType = LoopType.iterative;
170
- protected _visitedId: BinaryTreeNodeId[] = [];
171
- protected _visitedVal: Array<T> = [];
172
- protected _visitedNode: BinaryTreeNode<T>[] = [];
173
- protected _visitedCount: number[] = [];
174
- protected _visitedLeftSum: number[] = [];
175
-
176
- protected _resetResults() {
177
- this._visitedId = [];
178
- this._visitedVal = [];
179
- this._visitedNode = [];
180
- this._visitedCount = [];
181
- this._visitedLeftSum = [];
182
- }
183
-
184
- constructor(options?: {
185
- loopType?: LoopType,
186
- autoIncrementId?: boolean,
187
- isDuplicatedVal?: boolean
188
- }) {
189
- if (options !== undefined) {
190
- const {
191
- loopType = LoopType.iterative,
192
- autoIncrementId = false,
193
- isDuplicatedVal = false
194
- } = options;
195
- this._isDuplicatedVal = isDuplicatedVal;
196
- this._autoIncrementId = autoIncrementId;
197
- this._loopType = loopType;
198
- }
199
- }
200
-
207
+ /**
208
+ * The function creates a new binary tree node with the given id, value, and count, or returns null if the value is
209
+ * null.
210
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier for the binary tree node. It is of type
211
+ * `BinaryTreeNodeId`, which could be a string or a number, depending on how you want to identify your nodes.
212
+ * @param {T | null} val - The `val` parameter represents the value to be stored in the binary tree node. It can be of
213
+ * any type `T` or `null`.
214
+ * @param {number} [count] - The count parameter is an optional parameter that represents the number of occurrences of
215
+ * the value in the binary tree node. It is of type number.
216
+ * @returns The function `createNode` returns a `BinaryTreeNode<T>` object if the `val` parameter is not null.
217
+ * Otherwise, it returns null.
218
+ */
201
219
  createNode(id: BinaryTreeNodeId, val: T | null, count?: number): BinaryTreeNode<T> | null {
202
220
  return val !== null ? new BinaryTreeNode(id, val, count) : null;
203
221
  }
204
222
 
223
+ /**
224
+ * The clear function resets the state of an object by setting its properties to their initial values.
225
+ */
205
226
  clear() {
206
227
  this.root = null;
207
228
  this.size = 0;
@@ -209,11 +230,23 @@ export class BinaryTree<T> {
209
230
  this._maxId = -1;
210
231
  }
211
232
 
233
+ /**
234
+ * The function checks if the size of an object is equal to zero and returns a boolean value.
235
+ * @returns A boolean value indicating whether the size of the object is 0 or not.
236
+ */
212
237
  isEmpty(): boolean {
213
238
  return this.size === 0;
214
239
  }
215
240
 
216
- insertTo({newNode, parent}: { newNode: BinaryTreeNode<T> | null, parent: BinaryTreeNode<T> }) {
241
+ /**
242
+ * The function inserts a new node into a binary tree as the left or right child of a given parent node.
243
+ * @param {BinaryTreeNode<T> | null} newNode - The `newNode` parameter is an instance of the `BinaryTreeNode` class or
244
+ * `null`. It represents the node that needs to be inserted into the binary tree.
245
+ * @param parent - The `parent` parameter is a BinaryTreeNode object representing the parent node to which the new node
246
+ * will be inserted as a child.
247
+ * @returns The method returns the newly inserted node, either as the left child or the right child of the parent node.
248
+ */
249
+ putTo(newNode: BinaryTreeNode<T> | null, parent: BinaryTreeNode<T> ) {
217
250
  if (parent) {
218
251
  if (parent.left === undefined) {
219
252
  if (newNode) {
@@ -246,6 +279,17 @@ export class BinaryTree<T> {
246
279
  }
247
280
  }
248
281
 
282
+ /**
283
+ * The `put` function inserts a new node with a given ID and value into a binary tree, updating the count if the node
284
+ * already exists.
285
+ * @param {BinaryTreeNodeId} id - The id parameter is the identifier of the binary tree node. It is used to uniquely
286
+ * identify each node in the binary tree.
287
+ * @param {T} val - The value to be inserted into the binary tree.
288
+ * @param {number} [count] - The `count` parameter is an optional parameter that specifies the number of times the
289
+ * value should be inserted into the binary tree. If not provided, it defaults to 1.
290
+ * @returns The function `put` returns a `BinaryTreeNode<T>` object if a new node is inserted, or `null` if no new node
291
+ * is inserted, or `undefined` if the insertion fails.
292
+ */
249
293
  put(id: BinaryTreeNodeId, val: T, count?: number): BinaryTreeNode<T> | null | undefined {
250
294
  count = count ?? 1;
251
295
 
@@ -254,7 +298,7 @@ export class BinaryTree<T> {
254
298
  while (queue.length > 0) {
255
299
  const cur = queue.shift();
256
300
  if (cur) {
257
- const inserted = this.insertTo({newNode, parent: cur});
301
+ const inserted = this.putTo(newNode, cur);
258
302
  if (inserted !== undefined) return inserted;
259
303
  if (cur.left) queue.push(cur.left);
260
304
  if (cur.right) queue.push(cur.right);
@@ -288,6 +332,13 @@ export class BinaryTree<T> {
288
332
  return inserted;
289
333
  }
290
334
 
335
+ /**
336
+ * The `insertMany` function inserts multiple items into a binary tree and returns an array of the inserted nodes or
337
+ * null/undefined values.
338
+ * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
339
+ * array of `BinaryTreeNode<T>` objects.
340
+ * @returns The function `insertMany` returns an array of `BinaryTreeNode<T>`, `null`, or `undefined` values.
341
+ */
291
342
  insertMany(data: T[] | BinaryTreeNode<T>[]): (BinaryTreeNode<T> | null | undefined)[] {
292
343
  const inserted: (BinaryTreeNode<T> | null | undefined)[] = [];
293
344
  const map: Map<T | BinaryTreeNode<T>, number> = new Map();
@@ -328,11 +379,29 @@ export class BinaryTree<T> {
328
379
  return inserted;
329
380
  }
330
381
 
382
+ /**
383
+ * The `fill` function clears the current data and inserts new data, returning a boolean indicating if the insertion
384
+ * was successful.
385
+ * @param {T[] | BinaryTreeNode<T>[]} data - The `data` parameter can be either an array of elements of type `T` or an
386
+ * array of `BinaryTreeNode<T>` objects.
387
+ * @returns The method is returning a boolean value.
388
+ */
331
389
  fill(data: T[] | BinaryTreeNode<T>[]): boolean {
332
390
  this.clear();
333
391
  return data.length === this.insertMany(data).length;
334
392
  }
335
393
 
394
+ /**
395
+ * The function removes a node from a binary tree and returns information about the deleted node.
396
+ * @param {BinaryTreeNodeId} id - The `id` parameter is the identifier of the binary tree node that you want to remove.
397
+ * It is of type `BinaryTreeNodeId`.
398
+ * @param {boolean} [ignoreCount] - The `ignoreCount` parameter is an optional boolean parameter that determines
399
+ * whether to ignore the count of the node being removed. If `ignoreCount` is set to `true`, the count of the node will
400
+ * not be decremented and the overall count of the binary tree will not be updated. If `
401
+ * @returns An array of objects is being returned. Each object in the array has two properties: "deleted" and
402
+ * "needBalanced". The "deleted" property contains the deleted node or undefined if no node was deleted. The
403
+ * "needBalanced" property is always null.
404
+ */
336
405
  remove(id: BinaryTreeNodeId, ignoreCount?: boolean): BinaryTreeDeleted<T>[] {
337
406
  const nodes = this.getNodes(id, 'id', true);
338
407
  let node: BinaryTreeNode<T> | null | undefined = nodes[0];
@@ -369,6 +438,12 @@ export class BinaryTree<T> {
369
438
  return [{deleted: node, needBalanced: null}];
370
439
  }
371
440
 
441
+ /**
442
+ * The function calculates the depth of a binary tree node by traversing its parent nodes.
443
+ * @param node - BinaryTreeNode<T> - This is the node for which we want to calculate the depth. It is a generic type,
444
+ * meaning it can represent any type of data that we want to store in the node.
445
+ * @returns The depth of the given binary tree node.
446
+ */
372
447
  getDepth(node: BinaryTreeNode<T>): number {
373
448
  let depth = 0;
374
449
  while (node.parent) {
@@ -378,6 +453,14 @@ export class BinaryTree<T> {
378
453
  return depth;
379
454
  }
380
455
 
456
+ /**
457
+ * The `getHeight` function calculates the maximum height of a binary tree using either a recursive or iterative
458
+ * approach.
459
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
460
+ * `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the height of the binary tree.
461
+ * If no value is provided for `beginRoot`, the function will use the `root` property of the class instance as
462
+ * @returns the height of the binary tree.
463
+ */
381
464
  getHeight(beginRoot?: BinaryTreeNode<T> | null): number {
382
465
  beginRoot = beginRoot ?? this.root;
383
466
  if (!beginRoot) return -1;
@@ -393,8 +476,8 @@ export class BinaryTree<T> {
393
476
  return _getMaxHeight(beginRoot);
394
477
  } else {
395
478
  const stack: BinaryTreeNode<T>[] = [];
396
- let node: BinaryTreeNode<T> | null | undefined = beginRoot, last: BinaryTreeNode<T> | null = null,
397
- depths: Map<BinaryTreeNode<T>, number> = new Map();
479
+ let node: BinaryTreeNode<T> | null | undefined = beginRoot, last: BinaryTreeNode<T> | null = null;
480
+ const depths: Map<BinaryTreeNode<T>, number> = new Map();
398
481
 
399
482
  while (stack.length > 0 || node) {
400
483
  if (node) {
@@ -405,8 +488,8 @@ export class BinaryTree<T> {
405
488
  if (!node.right || last === node.right) {
406
489
  node = stack.pop();
407
490
  if (node) {
408
- let leftHeight = node.left ? depths.get(node.left) ?? -1 : -1;
409
- let rightHeight = node.right ? depths.get(node.right) ?? -1 : -1;
491
+ const leftHeight = node.left ? depths.get(node.left) ?? -1 : -1;
492
+ const rightHeight = node.right ? depths.get(node.right) ?? -1 : -1;
410
493
  depths.set(node, 1 + Math.max(leftHeight, rightHeight));
411
494
  last = node;
412
495
  node = null;
@@ -419,6 +502,14 @@ export class BinaryTree<T> {
419
502
  }
420
503
  }
421
504
 
505
+ /**
506
+ * The `getMinHeight` function calculates the minimum height of a binary tree using either a recursive or iterative
507
+ * approach.
508
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is an optional parameter of type
509
+ * `BinaryTreeNode<T> | null`. It represents the starting node from which to calculate the minimum height of the binary
510
+ * tree. If no value is provided for `beginRoot`, the function will use the root node of the binary tree.
511
+ * @returns The function `getMinHeight` returns the minimum height of the binary tree.
512
+ */
422
513
  getMinHeight(beginRoot?: BinaryTreeNode<T> | null): number {
423
514
  beginRoot = beginRoot || this.root;
424
515
  if (!beginRoot) return -1;
@@ -435,8 +526,8 @@ export class BinaryTree<T> {
435
526
  return _getMinHeight(beginRoot);
436
527
  } else {
437
528
  const stack: BinaryTreeNode<T>[] = [];
438
- let node: BinaryTreeNode<T> | null | undefined = beginRoot, last: BinaryTreeNode<T> | null = null,
439
- depths: Map<BinaryTreeNode<T>, number> = new Map();
529
+ let node: BinaryTreeNode<T> | null | undefined = beginRoot, last: BinaryTreeNode<T> | null = null;
530
+ const depths: Map<BinaryTreeNode<T>, number> = new Map();
440
531
 
441
532
  while (stack.length > 0 || node) {
442
533
  if (node) {
@@ -447,8 +538,8 @@ export class BinaryTree<T> {
447
538
  if (!node.right || last === node.right) {
448
539
  node = stack.pop();
449
540
  if (node) {
450
- let leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
451
- let rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
541
+ const leftMinHeight = node.left ? depths.get(node.left) ?? -1 : -1;
542
+ const rightMinHeight = node.right ? depths.get(node.right) ?? -1 : -1;
452
543
  depths.set(node, 1 + Math.min(leftMinHeight, rightMinHeight));
453
544
  last = node;
454
545
  node = null;
@@ -461,10 +552,28 @@ export class BinaryTree<T> {
461
552
  }
462
553
  }
463
554
 
555
+ /**
556
+ * The function checks if a binary tree is balanced by comparing the minimum height and the maximum height of the tree.
557
+ * @param {BinaryTreeNode<T> | null} [beginRoot] - The `beginRoot` parameter is the root node of a binary tree. It is
558
+ * of type `BinaryTreeNode<T> | null`, which means it can either be a `BinaryTreeNode` object or `null`.
559
+ * @returns The method is returning a boolean value.
560
+ */
464
561
  isBalanced(beginRoot?: BinaryTreeNode<T> | null): boolean {
465
562
  return (this.getMinHeight(beginRoot) + 1 >= this.getHeight(beginRoot));
466
563
  }
467
564
 
565
+ /**
566
+ * The function `getNodes` returns an array of binary tree nodes that match a given property value, with options for
567
+ * searching recursively or iteratively.
568
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
569
+ * generic type `T`. It represents the property of the binary tree node that you want to search for.
570
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
571
+ * specifies the property name to use when searching for nodes. If not provided, it defaults to 'id'.
572
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
573
+ * return only one node that matches the `nodeProperty` or `propertyName` criteria. If `onlyOne` is set to `true`, the
574
+ * function will stop traversing the tree and return the first matching node. If `
575
+ * @returns The function `getNodes` returns an array of `BinaryTreeNode<T> | null | undefined` objects.
576
+ */
468
577
  getNodes(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
469
578
  if (!this.root) return [] as null[];
470
579
  propertyName = propertyName ?? 'id';
@@ -495,15 +604,40 @@ export class BinaryTree<T> {
495
604
  return result;
496
605
  }
497
606
 
607
+ /**
608
+ * The function checks if a binary tree node has a specific property or if any node in the tree has a specific
609
+ * property.
610
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
611
+ * generic type `T`. It represents the property of a binary tree node that you want to check.
612
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
613
+ * specifies the name of the property to check for in the nodes.
614
+ * @returns a boolean value.
615
+ */
498
616
  has(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName): boolean {
499
617
  return this.getNodes(nodeProperty, propertyName).length > 0;
500
618
  }
501
619
 
620
+ /**
621
+ * The function returns the first binary tree node that matches the given property name and value, or null if no match
622
+ * is found.
623
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter can be either a `BinaryTreeNodeId` or a
624
+ * generic type `T`. It represents the property of the binary tree node that you want to search for.
625
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
626
+ * specifies the property of the binary tree node to search for. If not provided, it defaults to `'id'`.
627
+ * @returns a BinaryTreeNode object or null.
628
+ */
502
629
  get(nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName): BinaryTreeNode<T> | null {
503
630
  propertyName = propertyName ?? 'id';
504
631
  return this.getNodes(nodeProperty, propertyName, true)[0] ?? null;
505
632
  }
506
633
 
634
+ /**
635
+ * The function getPathToRoot returns an array of BinaryTreeNode objects representing the path from a given node to the
636
+ * root of a binary tree.
637
+ * @param node - The `node` parameter is a BinaryTreeNode object.
638
+ * @returns The function `getPathToRoot` returns an array of `BinaryTreeNode<T>` objects, representing the path from
639
+ * the given `node` to the root of the binary tree.
640
+ */
507
641
  getPathToRoot(node: BinaryTreeNode<T>): BinaryTreeNode<T>[] {
508
642
  const result: BinaryTreeNode<T>[] = [];
509
643
  while (node.parent) {
@@ -514,76 +648,18 @@ export class BinaryTree<T> {
514
648
  return result;
515
649
  }
516
650
 
517
- protected _pushByPropertyNameStopOrNot(cur: BinaryTreeNode<T>, result: (BinaryTreeNode<T> | null | undefined)[], nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
518
- switch (propertyName) {
519
- case 'id':
520
- if (cur.id === nodeProperty) {
521
- result.push(cur);
522
- return !!onlyOne;
523
- }
524
- break;
525
- case 'count':
526
- if (cur.count === nodeProperty) {
527
- result.push(cur);
528
- return !!onlyOne;
529
- }
530
- break;
531
- case 'val':
532
- if (cur.val === nodeProperty) {
533
- result.push(cur);
534
- return !!onlyOne;
535
- }
536
- break;
537
- default:
538
- if (cur.id === nodeProperty) {
539
- result.push(cur);
540
- return !!onlyOne;
541
- }
542
- break;
543
- }
544
- }
545
-
546
- protected _accumulatedByPropertyName(node: BinaryTreeNode<T>, nodeOrPropertyName ?: NodeOrPropertyName) {
547
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
548
-
549
- switch (nodeOrPropertyName) {
550
- case 'id':
551
- this._visitedId.push(node.id);
552
- break;
553
- case 'val':
554
- this._visitedVal.push(node.val);
555
- break;
556
- case 'node':
557
- this._visitedNode.push(node);
558
- break;
559
- case 'count':
560
- this._visitedCount.push(node.count);
561
- break;
562
- default:
563
- this._visitedId.push(node.id);
564
- break;
565
- }
566
- }
567
-
568
- protected _getResultByPropertyName(nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
569
- nodeOrPropertyName = nodeOrPropertyName ?? 'id';
570
-
571
- switch (nodeOrPropertyName) {
572
- case 'id':
573
- return this._visitedId;
574
- case 'val':
575
- return this._visitedVal;
576
- case 'node':
577
- return this._visitedNode;
578
- case 'count':
579
- return this._visitedCount;
580
- default:
581
- return this._visitedId;
582
- }
583
- }
584
-
585
651
  getLeftMost(): BinaryTreeNode<T> | null;
652
+
586
653
  getLeftMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
654
+
655
+ /**
656
+ * The `getLeftMost` function returns the leftmost node in a binary tree, either recursively or iteratively using tail
657
+ * recursion optimization.
658
+ * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
659
+ * | null`. It represents the starting node from which to find the leftmost node in a binary tree. If no node is
660
+ * provided, the function will use the root node of the binary tree.
661
+ * @returns The `getLeftMost` function returns the leftmost node in a binary tree.
662
+ */
587
663
  getLeftMost(node?: BinaryTreeNode<T> | null): BinaryTreeNode<T> | null {
588
664
  node = node ?? this.root;
589
665
  if (!node) return node;
@@ -598,7 +674,7 @@ export class BinaryTree<T> {
598
674
  return _traverse(node);
599
675
  } else {
600
676
  // Indirect implementation of iteration using tail recursion optimization
601
- const _traverse = trampoline((cur: BinaryTreeNode<T>): ThunkOrValue<BinaryTreeNode<T> | null> => {
677
+ const _traverse = trampoline((cur: BinaryTreeNode<T>) => {
602
678
  if (!cur.left) return cur;
603
679
  return _traverse.cont(cur.left);
604
680
  });
@@ -608,7 +684,17 @@ export class BinaryTree<T> {
608
684
  }
609
685
 
610
686
  getRightMost(): BinaryTreeNode<T> | null;
687
+
611
688
  getRightMost(node: BinaryTreeNode<T>): BinaryTreeNode<T>;
689
+
690
+ /**
691
+ * The `getRightMost` function returns the rightmost node in a binary tree, either recursively or iteratively using
692
+ * tail recursion optimization.
693
+ * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
694
+ * | null`. It represents the starting node from which to find the rightmost node in a binary tree. If no node is
695
+ * provided, the function will use the root node of the binary tree.
696
+ * @returns The `getRightMost` function returns the rightmost node in a binary tree.
697
+ */
612
698
  getRightMost(node?: BinaryTreeNode<T> | null): BinaryTreeNode<T> | null {
613
699
  node = node ?? this.root;
614
700
  if (!node) return node;
@@ -622,7 +708,7 @@ export class BinaryTree<T> {
622
708
  return _traverse(node);
623
709
  } else {
624
710
  // Indirect implementation of iteration using tail recursion optimization
625
- const _traverse = trampoline((cur: BinaryTreeNode<T>): ThunkOrValue<BinaryTreeNode<T> | null> => {
711
+ const _traverse = trampoline((cur: BinaryTreeNode<T>) => {
626
712
  if (!cur.right) return cur;
627
713
  return _traverse.cont(cur.right);
628
714
  });
@@ -632,6 +718,14 @@ export class BinaryTree<T> {
632
718
  }
633
719
 
634
720
  // --- start additional methods ---
721
+ /**
722
+ * The `isBST` function checks if a binary tree is a binary search tree.
723
+ * @param {BinaryTreeNode<T> | null} [node] - The `node` parameter is an optional parameter of type `BinaryTreeNode<T>
724
+ * | null`. It represents the root node of the binary search tree (BST) that we want to check for validity. If no node
725
+ * is provided, the function will default to using the root node of the BST instance that
726
+ * @returns The `isBST` function returns a boolean value. It returns `true` if the binary tree is a valid binary search
727
+ * tree, and `false` otherwise.
728
+ */
635
729
  isBST(node?: BinaryTreeNode<T> | null): boolean {
636
730
  node = node ?? this.root;
637
731
  if (!node) return true;
@@ -653,7 +747,7 @@ export class BinaryTree<T> {
653
747
  curr = curr.left;
654
748
  }
655
749
  curr = stack.pop()!;
656
- if (prev >= curr.id) return false;
750
+ if (!(curr) || prev >= curr.id) return false;
657
751
  prev = curr.id;
658
752
  curr = curr.right;
659
753
  }
@@ -661,6 +755,14 @@ export class BinaryTree<T> {
661
755
  }
662
756
  }
663
757
 
758
+ /**
759
+ * The function calculates the size and count of a subtree in a binary tree using either recursive or iterative
760
+ * traversal.
761
+ * @param {BinaryTreeNode<T> | null | undefined} subTreeRoot - The `subTreeRoot` parameter is the root node of a binary
762
+ * tree.
763
+ * @returns The function `getSubTreeSizeAndCount` returns an array `[number, number]`. The first element of the array
764
+ * represents the size of the subtree, and the second element represents the count of the nodes in the subtree.
765
+ */
664
766
  getSubTreeSizeAndCount(subTreeRoot: BinaryTreeNode<T> | null | undefined) {
665
767
  const res: [number, number] = [0, 0];
666
768
  if (!subTreeRoot) return res;
@@ -690,6 +792,16 @@ export class BinaryTree<T> {
690
792
  }
691
793
  }
692
794
 
795
+ /**
796
+ * The function `subTreeSum` calculates the sum of a specified property in a binary tree, either recursively or
797
+ * iteratively.
798
+ * @param subTreeRoot - The subTreeRoot parameter is the root node of the subtree for which you want to calculate the
799
+ * sum.
800
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
801
+ * specifies the property of the `BinaryTreeNode` object to use for calculating the sum. If `propertyName` is not
802
+ * provided, it defaults to `'val'`.
803
+ * @returns a number, which is the sum of the values of the nodes in the subtree rooted at `subTreeRoot`.
804
+ */
693
805
  subTreeSum(subTreeRoot: BinaryTreeNode<T>, propertyName ?: BinaryTreeNodePropertyName): number {
694
806
  propertyName = propertyName ?? 'val';
695
807
  if (!subTreeRoot) return 0;
@@ -737,6 +849,15 @@ export class BinaryTree<T> {
737
849
  return sum;
738
850
  }
739
851
 
852
+ /**
853
+ * The function `subTreeAdd` adds a specified delta value to a property of each node in a binary tree.
854
+ * @param subTreeRoot - The `subTreeRoot` parameter is the root node of the subtree where the values will be modified.
855
+ * @param {number} delta - The `delta` parameter is a number that represents the amount by which the property value of
856
+ * each node in the subtree should be increased or decreased.
857
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
858
+ * specifies the property of the `BinaryTreeNode` that should be modified. It defaults to `'id'` if not provided.
859
+ * @returns a boolean value, which is `true`.
860
+ */
740
861
  subTreeAdd(subTreeRoot: BinaryTreeNode<T>, delta: number, propertyName ?: BinaryTreeNodePropertyName): boolean {
741
862
  propertyName = propertyName ?? 'id';
742
863
  if (!subTreeRoot) return false;
@@ -779,10 +900,24 @@ export class BinaryTree<T> {
779
900
  }
780
901
 
781
902
  BFS(): BinaryTreeNodeId[];
903
+
782
904
  BFS(nodeOrPropertyName: 'id'): BinaryTreeNodeId[];
905
+
783
906
  BFS(nodeOrPropertyName: 'val'): T[];
907
+
784
908
  BFS(nodeOrPropertyName: 'node'): BinaryTreeNode<T>[];
909
+
785
910
  BFS(nodeOrPropertyName: 'count'): number[];
911
+
912
+ /**
913
+ * The BFS function performs a breadth-first search on a binary tree and returns the results based on a specified node
914
+ * or property name.
915
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
916
+ * represents either a node or a property name. If a node is provided, the breadth-first search algorithm will be
917
+ * performed starting from that node. If a property name is provided, the breadth-first search algorithm will be
918
+ * performed starting from the root node
919
+ * @returns an object of type `ResultsByProperty<T>`.
920
+ */
786
921
  BFS(nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
787
922
  nodeOrPropertyName = nodeOrPropertyName ?? 'id';
788
923
  this._resetResults();
@@ -801,10 +936,27 @@ export class BinaryTree<T> {
801
936
  }
802
937
 
803
938
  DFS(): BinaryTreeNodeId[];
939
+
804
940
  DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
941
+
805
942
  DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
943
+
806
944
  DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
945
+
807
946
  DFS(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
947
+
948
+ /**
949
+ * The DFS function performs a depth-first search traversal on a binary tree and returns the results based on the
950
+ * specified pattern and node or property name.
951
+ * @param {'in' | 'pre' | 'post'} [pattern] - The "pattern" parameter is used to specify the order in which the nodes
952
+ * of a binary tree are traversed during the Depth-First Search (DFS) algorithm. It can take one of three values: 'in',
953
+ * 'pre', or 'post'.
954
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is a string that represents
955
+ * either the name of a property in the `BinaryTreeNode` object or the value of the `id` property in the
956
+ * `BinaryTreeNode` object. This parameter is used to accumulate the results based on the specified property name. If
957
+ * no value
958
+ * @returns an object of type `ResultsByProperty<T>`.
959
+ */
808
960
  DFS(pattern ?: 'in' | 'pre' | 'post', nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
809
961
  pattern = pattern ?? 'in';
810
962
  nodeOrPropertyName = nodeOrPropertyName ?? 'id';
@@ -834,9 +986,13 @@ export class BinaryTree<T> {
834
986
  }
835
987
 
836
988
  DFSIterative(): BinaryTreeNodeId[];
989
+
837
990
  DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
991
+
838
992
  DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
993
+
839
994
  DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
995
+
840
996
  DFSIterative(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
841
997
 
842
998
  /**
@@ -889,10 +1045,27 @@ export class BinaryTree<T> {
889
1045
  }
890
1046
 
891
1047
  levelIterative(node: BinaryTreeNode<T> | null): BinaryTreeNodeId[];
1048
+
892
1049
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1050
+
893
1051
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'val'): T[];
1052
+
894
1053
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
1054
+
895
1055
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[];
1056
+
1057
+ /**
1058
+ * The `levelIterative` function performs a level-order traversal on a binary tree and returns the values of the nodes
1059
+ * in an array, based on a specified property name.
1060
+ * @param {BinaryTreeNode<T> | null} node - The `node` parameter is a BinaryTreeNode object representing the starting
1061
+ * node for the level order traversal. It can be null if no specific node is provided, in which case the root node of
1062
+ * the tree is used as the starting node.
1063
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1064
+ * can be either a `BinaryTreeNode` property name or the string `'id'`. If a property name is provided, the function
1065
+ * will accumulate results based on that property. If no property name is provided, the function will default to
1066
+ * accumulating results
1067
+ * @returns The function `levelIterative` returns an object of type `ResultsByProperty<T>`.
1068
+ */
896
1069
  levelIterative(node: BinaryTreeNode<T> | null, nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
897
1070
  nodeOrPropertyName = nodeOrPropertyName || 'id';
898
1071
  node = node || this.root;
@@ -918,10 +1091,24 @@ export class BinaryTree<T> {
918
1091
  }
919
1092
 
920
1093
  listLevels(node: BinaryTreeNode<T> | null): BinaryTreeNodeId[][];
1094
+
921
1095
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[][];
1096
+
922
1097
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'val'): T[][];
1098
+
923
1099
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[][];
1100
+
924
1101
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: 'count'): number[][];
1102
+
1103
+ /**
1104
+ * The `listLevels` function collects nodes from a binary tree by a specified property and organizes them into levels.
1105
+ * @param {BinaryTreeNode<T> | null} node - The `node` parameter is a BinaryTreeNode object or null. It represents the
1106
+ * root node of a binary tree. If it is null, the function will use the root node of the current binary tree instance.
1107
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is an optional parameter that
1108
+ * specifies the property of the `BinaryTreeNode` object to collect at each level. It can be one of the following
1109
+ * values:
1110
+ * @returns The function `listLevels` returns a 2D array of `ResultByProperty<T>` objects.
1111
+ */
925
1112
  listLevels(node: BinaryTreeNode<T> | null, nodeOrPropertyName?: NodeOrPropertyName): ResultByProperty<T>[][] {
926
1113
  nodeOrPropertyName = nodeOrPropertyName || 'id';
927
1114
  node = node || this.root;
@@ -975,11 +1162,18 @@ export class BinaryTree<T> {
975
1162
  return levelsNodes;
976
1163
  }
977
1164
 
1165
+ /**
1166
+ * The function returns the predecessor of a given node in a binary tree.
1167
+ * @param node - The parameter `node` is a BinaryTreeNode object, representing a node in a binary tree.
1168
+ * @returns the predecessor of the given node in a binary tree.
1169
+ */
978
1170
  getPredecessor(node: BinaryTreeNode<T>): BinaryTreeNode<T> {
979
1171
  if (node.left) {
980
- let predecessor: BinaryTreeNode<T> | null = node.left;
981
- while (predecessor.right && predecessor.right !== node) {
982
- predecessor = predecessor.right;
1172
+ let predecessor: BinaryTreeNode<T> | null | undefined = node.left;
1173
+ while (!(predecessor) || predecessor.right && predecessor.right !== node) {
1174
+ if (predecessor) {
1175
+ predecessor = predecessor.right;
1176
+ }
983
1177
  }
984
1178
  return predecessor;
985
1179
  } else {
@@ -988,15 +1182,26 @@ export class BinaryTree<T> {
988
1182
  }
989
1183
 
990
1184
  morris(): BinaryTreeNodeId[];
1185
+
991
1186
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'id'): BinaryTreeNodeId[];
1187
+
992
1188
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'val'): T[];
1189
+
993
1190
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'node'): BinaryTreeNode<T>[];
1191
+
994
1192
  morris(pattern?: DFSOrderPattern, nodeOrPropertyName?: 'count'): number[];
1193
+
995
1194
  /**
1195
+ * The `morris` function performs an in-order, pre-order, or post-order traversal on a binary tree using the Morris
1196
+ * traversal algorithm and returns the results based on the specified property name.
996
1197
  * The time complexity of Morris traversal is O(n), it's may slower than others
997
1198
  * The space complexity Morris traversal is O(1) because no using stack
998
- * @param pattern
999
- * @param nodeOrPropertyName
1199
+ * @param {'in' | 'pre' | 'post'} [pattern] - The `pattern` parameter is an optional parameter that determines the
1200
+ * traversal pattern of the binary tree. It can have one of three values:
1201
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The `nodeOrPropertyName` parameter is used to specify the
1202
+ * property of the nodes that you want to retrieve in the results. It can be either the node itself or the name of the
1203
+ * property. If not provided, it defaults to `'id'`.
1204
+ * @returns The function `morris` returns an object of type `ResultsByProperty<T>`.
1000
1205
  */
1001
1206
  morris(pattern?: 'in' | 'pre' | 'post', nodeOrPropertyName?: NodeOrPropertyName): ResultsByProperty<T> {
1002
1207
  if (this.root === null) return [];
@@ -1084,5 +1289,115 @@ export class BinaryTree<T> {
1084
1289
  return this._getResultByPropertyName(nodeOrPropertyName);
1085
1290
  }
1086
1291
 
1292
+ /**
1293
+ * The function resets the values of several arrays used for tracking visited nodes and their properties.
1294
+ */
1295
+ protected _resetResults() {
1296
+ this._visitedId = [];
1297
+ this._visitedVal = [];
1298
+ this._visitedNode = [];
1299
+ this._visitedCount = [];
1300
+ this._visitedLeftSum = [];
1301
+ }
1302
+
1303
+ /**
1304
+ * The function checks if a given property of a binary tree node matches a specified value, and if so, adds the node to
1305
+ * a result array.
1306
+ * @param cur - The current binary tree node that is being checked.
1307
+ * @param {(BinaryTreeNode<T> | null | undefined)[]} result - An array that stores the matching nodes found during the
1308
+ * traversal.
1309
+ * @param {BinaryTreeNodeId | T} nodeProperty - The `nodeProperty` parameter is the value that we are searching for in
1310
+ * the binary tree nodes. It can be either the `id`, `count`, or `val` property of the node.
1311
+ * @param {BinaryTreeNodePropertyName} [propertyName] - The `propertyName` parameter is an optional parameter that
1312
+ * specifies the property of the `BinaryTreeNode` object that you want to compare with the `nodeProperty` value. It can
1313
+ * be one of the following values: 'id', 'count', or 'val'. If no `propertyName` is provided,
1314
+ * @param {boolean} [onlyOne] - The `onlyOne` parameter is an optional boolean parameter that determines whether to
1315
+ * stop after finding the first matching node or continue searching for all matching nodes. If `onlyOne` is set to
1316
+ * `true`, the function will stop after finding the first matching node and return `true`. If `onlyOne
1317
+ * @returns a boolean value indicating whether or not a node was pushed into the result array.
1318
+ */
1319
+ protected _pushByPropertyNameStopOrNot(cur: BinaryTreeNode<T>, result: (BinaryTreeNode<T> | null | undefined)[], nodeProperty: BinaryTreeNodeId | T, propertyName ?: BinaryTreeNodePropertyName, onlyOne ?: boolean) {
1320
+ switch (propertyName) {
1321
+ case 'id':
1322
+ if (cur.id === nodeProperty) {
1323
+ result.push(cur);
1324
+ return !!onlyOne;
1325
+ }
1326
+ break;
1327
+ case 'count':
1328
+ if (cur.count === nodeProperty) {
1329
+ result.push(cur);
1330
+ return !!onlyOne;
1331
+ }
1332
+ break;
1333
+ case 'val':
1334
+ if (cur.val === nodeProperty) {
1335
+ result.push(cur);
1336
+ return !!onlyOne;
1337
+ }
1338
+ break;
1339
+ default:
1340
+ if (cur.id === nodeProperty) {
1341
+ result.push(cur);
1342
+ return !!onlyOne;
1343
+ }
1344
+ break;
1345
+ }
1346
+ }
1347
+
1348
+ /**
1349
+ * The function `_accumulatedByPropertyName` pushes a property value of a binary tree node into an array based on the
1350
+ * provided property name or a default property name.
1351
+ * @param node - The `node` parameter is of type `BinaryTreeNode<T>`, which represents a node in a binary tree.
1352
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1353
+ * can be either a string representing a property name or a reference to a node object. If it is a string, it specifies
1354
+ * the property name of the node that should be accumulated. If it is a node object, it specifies the node itself
1355
+ */
1356
+ protected _accumulatedByPropertyName(node: BinaryTreeNode<T>, nodeOrPropertyName ?: NodeOrPropertyName) {
1357
+ nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1358
+
1359
+ switch (nodeOrPropertyName) {
1360
+ case 'id':
1361
+ this._visitedId.push(node.id);
1362
+ break;
1363
+ case 'val':
1364
+ this._visitedVal.push(node.val);
1365
+ break;
1366
+ case 'node':
1367
+ this._visitedNode.push(node);
1368
+ break;
1369
+ case 'count':
1370
+ this._visitedCount.push(node.count);
1371
+ break;
1372
+ default:
1373
+ this._visitedId.push(node.id);
1374
+ break;
1375
+ }
1376
+ }
1377
+
1378
+ /**
1379
+ * The function `_getResultByPropertyName` returns different results based on the provided property name or defaulting
1380
+ * to 'id'.
1381
+ * @param {NodeOrPropertyName} [nodeOrPropertyName] - The parameter `nodeOrPropertyName` is an optional parameter that
1382
+ * can accept a value of type `NodeOrPropertyName`.
1383
+ * @returns The method returns an object of type `ResultsByProperty<T>`.
1384
+ */
1385
+ protected _getResultByPropertyName(nodeOrPropertyName ?: NodeOrPropertyName): ResultsByProperty<T> {
1386
+ nodeOrPropertyName = nodeOrPropertyName ?? 'id';
1387
+
1388
+ switch (nodeOrPropertyName) {
1389
+ case 'id':
1390
+ return this._visitedId;
1391
+ case 'val':
1392
+ return this._visitedVal;
1393
+ case 'node':
1394
+ return this._visitedNode;
1395
+ case 'count':
1396
+ return this._visitedCount;
1397
+ default:
1398
+ return this._visitedId;
1399
+ }
1400
+ }
1401
+
1087
1402
  // --- end additional methods ---
1088
1403
  }