heap-typed 2.1.1 → 2.1.2

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 (281) hide show
  1. package/dist/cjs/index.cjs +1020 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/esm/index.mjs +1012 -0
  4. package/dist/esm/index.mjs.map +1 -0
  5. package/dist/types/data-structures/base/index.d.ts +2 -1
  6. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +182 -2
  7. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +135 -2
  8. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +291 -2
  9. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -1
  10. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +754 -29
  11. package/dist/types/data-structures/binary-tree/bst.d.ts +413 -12
  12. package/dist/types/data-structures/binary-tree/index.d.ts +3 -2
  13. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +208 -3
  14. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -1
  15. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +190 -2
  16. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +270 -2
  17. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -14
  18. package/dist/types/data-structures/graph/directed-graph.d.ts +207 -1
  19. package/dist/types/data-structures/graph/index.d.ts +2 -1
  20. package/dist/types/data-structures/graph/map-graph.d.ts +78 -1
  21. package/dist/types/data-structures/graph/undirected-graph.d.ts +188 -1
  22. package/dist/types/data-structures/hash/hash-map.d.ts +345 -19
  23. package/dist/types/data-structures/hash/index.d.ts +0 -1
  24. package/dist/types/data-structures/heap/heap.d.ts +503 -5
  25. package/dist/types/data-structures/heap/index.d.ts +2 -0
  26. package/dist/types/data-structures/heap/max-heap.d.ts +32 -1
  27. package/dist/types/data-structures/heap/min-heap.d.ts +33 -1
  28. package/dist/types/data-structures/index.d.ts +7 -7
  29. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +769 -2
  30. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +451 -2
  31. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -4
  32. package/dist/types/data-structures/matrix/index.d.ts +1 -1
  33. package/dist/types/data-structures/matrix/matrix.d.ts +168 -7
  34. package/dist/types/data-structures/matrix/navigator.d.ts +54 -13
  35. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -1
  36. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -1
  37. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -2
  38. package/dist/types/data-structures/queue/deque.d.ts +431 -4
  39. package/dist/types/data-structures/queue/queue.d.ts +308 -4
  40. package/dist/types/data-structures/stack/stack.d.ts +306 -2
  41. package/dist/types/data-structures/tree/tree.d.ts +62 -1
  42. package/dist/types/data-structures/trie/trie.d.ts +350 -4
  43. package/dist/types/index.d.ts +11 -2
  44. package/dist/{interfaces → types/interfaces}/binary-tree.d.ts +1 -1
  45. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  46. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  47. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  48. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  49. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  50. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  51. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  52. package/dist/{data-structures → types/types/data-structures}/binary-tree/index.d.ts +2 -3
  53. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  54. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  55. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  56. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  57. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  58. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  59. package/dist/{data-structures → types/types/data-structures}/graph/index.d.ts +1 -2
  60. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  61. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  62. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  63. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  64. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  65. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  66. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  67. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  68. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  69. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  70. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  71. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  72. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  73. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  74. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  75. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  76. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  77. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  78. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  79. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  80. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  81. package/dist/types/types/index.d.ts +3 -0
  82. package/dist/types/types/utils/index.d.ts +2 -0
  83. package/dist/types/types/utils/utils.d.ts +22 -0
  84. package/dist/types/utils/index.d.ts +1 -1
  85. package/dist/types/utils/utils.d.ts +209 -22
  86. package/dist/umd/heap-typed.js +1023 -0
  87. package/dist/umd/heap-typed.js.map +1 -0
  88. package/dist/umd/heap-typed.min.js +22 -0
  89. package/dist/umd/heap-typed.min.js.map +1 -0
  90. package/package.json +25 -5
  91. package/src/data-structures/binary-tree/avl-tree-counter.ts +4 -4
  92. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +1 -1
  93. package/src/data-structures/binary-tree/avl-tree.ts +2 -2
  94. package/src/data-structures/binary-tree/binary-tree.ts +4 -4
  95. package/src/data-structures/binary-tree/bst.ts +1 -1
  96. package/src/data-structures/binary-tree/red-black-tree.ts +2 -2
  97. package/src/data-structures/binary-tree/tree-counter.ts +4 -4
  98. package/src/data-structures/binary-tree/tree-multi-map.ts +1 -1
  99. package/src/data-structures/heap/heap.ts +5 -5
  100. package/src/data-structures/linked-list/singly-linked-list.ts +2 -2
  101. package/src/interfaces/binary-tree.ts +1 -1
  102. package/tsconfig.base.json +23 -0
  103. package/tsconfig.json +8 -34
  104. package/tsconfig.test.json +8 -0
  105. package/tsconfig.types.json +15 -0
  106. package/tsup.config.js +28 -0
  107. package/tsup.node.config.js +37 -0
  108. package/dist/common/index.js +0 -28
  109. package/dist/constants/index.js +0 -8
  110. package/dist/data-structures/base/index.d.ts +0 -2
  111. package/dist/data-structures/base/index.js +0 -18
  112. package/dist/data-structures/base/iterable-element-base.js +0 -243
  113. package/dist/data-structures/base/iterable-entry-base.js +0 -183
  114. package/dist/data-structures/base/linear-base.js +0 -415
  115. package/dist/data-structures/binary-tree/avl-tree-counter.d.ts +0 -182
  116. package/dist/data-structures/binary-tree/avl-tree-counter.js +0 -374
  117. package/dist/data-structures/binary-tree/avl-tree-multi-map.d.ts +0 -135
  118. package/dist/data-structures/binary-tree/avl-tree-multi-map.js +0 -250
  119. package/dist/data-structures/binary-tree/avl-tree.d.ts +0 -291
  120. package/dist/data-structures/binary-tree/avl-tree.js +0 -611
  121. package/dist/data-structures/binary-tree/binary-indexed-tree.d.ts +0 -174
  122. package/dist/data-structures/binary-tree/binary-indexed-tree.js +0 -294
  123. package/dist/data-structures/binary-tree/binary-tree.d.ts +0 -754
  124. package/dist/data-structures/binary-tree/binary-tree.js +0 -1925
  125. package/dist/data-structures/binary-tree/bst.d.ts +0 -413
  126. package/dist/data-structures/binary-tree/bst.js +0 -903
  127. package/dist/data-structures/binary-tree/index.js +0 -26
  128. package/dist/data-structures/binary-tree/red-black-tree.d.ts +0 -208
  129. package/dist/data-structures/binary-tree/red-black-tree.js +0 -546
  130. package/dist/data-structures/binary-tree/segment-tree.d.ts +0 -160
  131. package/dist/data-structures/binary-tree/segment-tree.js +0 -297
  132. package/dist/data-structures/binary-tree/tree-counter.d.ts +0 -190
  133. package/dist/data-structures/binary-tree/tree-counter.js +0 -413
  134. package/dist/data-structures/binary-tree/tree-multi-map.d.ts +0 -270
  135. package/dist/data-structures/binary-tree/tree-multi-map.js +0 -384
  136. package/dist/data-structures/graph/abstract-graph.d.ts +0 -340
  137. package/dist/data-structures/graph/abstract-graph.js +0 -896
  138. package/dist/data-structures/graph/directed-graph.d.ts +0 -207
  139. package/dist/data-structures/graph/directed-graph.js +0 -525
  140. package/dist/data-structures/graph/index.js +0 -20
  141. package/dist/data-structures/graph/map-graph.d.ts +0 -78
  142. package/dist/data-structures/graph/map-graph.js +0 -107
  143. package/dist/data-structures/graph/undirected-graph.d.ts +0 -188
  144. package/dist/data-structures/graph/undirected-graph.js +0 -424
  145. package/dist/data-structures/hash/hash-map.d.ts +0 -345
  146. package/dist/data-structures/hash/hash-map.js +0 -692
  147. package/dist/data-structures/hash/index.d.ts +0 -1
  148. package/dist/data-structures/hash/index.js +0 -17
  149. package/dist/data-structures/heap/heap.d.ts +0 -503
  150. package/dist/data-structures/heap/heap.js +0 -901
  151. package/dist/data-structures/heap/index.d.ts +0 -3
  152. package/dist/data-structures/heap/index.js +0 -19
  153. package/dist/data-structures/heap/max-heap.d.ts +0 -32
  154. package/dist/data-structures/heap/max-heap.js +0 -40
  155. package/dist/data-structures/heap/min-heap.d.ts +0 -33
  156. package/dist/data-structures/heap/min-heap.js +0 -31
  157. package/dist/data-structures/index.js +0 -28
  158. package/dist/data-structures/linked-list/doubly-linked-list.d.ts +0 -769
  159. package/dist/data-structures/linked-list/doubly-linked-list.js +0 -1111
  160. package/dist/data-structures/linked-list/index.js +0 -19
  161. package/dist/data-structures/linked-list/singly-linked-list.d.ts +0 -451
  162. package/dist/data-structures/linked-list/singly-linked-list.js +0 -850
  163. package/dist/data-structures/linked-list/skip-linked-list.d.ts +0 -27
  164. package/dist/data-structures/linked-list/skip-linked-list.js +0 -144
  165. package/dist/data-structures/matrix/index.js +0 -18
  166. package/dist/data-structures/matrix/matrix.d.ts +0 -168
  167. package/dist/data-structures/matrix/matrix.js +0 -448
  168. package/dist/data-structures/matrix/navigator.d.ts +0 -55
  169. package/dist/data-structures/matrix/navigator.js +0 -111
  170. package/dist/data-structures/priority-queue/index.js +0 -19
  171. package/dist/data-structures/priority-queue/max-priority-queue.d.ts +0 -27
  172. package/dist/data-structures/priority-queue/max-priority-queue.js +0 -34
  173. package/dist/data-structures/priority-queue/min-priority-queue.d.ts +0 -26
  174. package/dist/data-structures/priority-queue/min-priority-queue.js +0 -24
  175. package/dist/data-structures/priority-queue/priority-queue.d.ts +0 -15
  176. package/dist/data-structures/priority-queue/priority-queue.js +0 -20
  177. package/dist/data-structures/queue/deque.d.ts +0 -431
  178. package/dist/data-structures/queue/deque.js +0 -879
  179. package/dist/data-structures/queue/index.js +0 -18
  180. package/dist/data-structures/queue/queue.d.ts +0 -308
  181. package/dist/data-structures/queue/queue.js +0 -473
  182. package/dist/data-structures/stack/index.js +0 -17
  183. package/dist/data-structures/stack/stack.d.ts +0 -306
  184. package/dist/data-structures/stack/stack.js +0 -401
  185. package/dist/data-structures/tree/index.js +0 -17
  186. package/dist/data-structures/tree/tree.d.ts +0 -62
  187. package/dist/data-structures/tree/tree.js +0 -107
  188. package/dist/data-structures/trie/index.js +0 -17
  189. package/dist/data-structures/trie/trie.d.ts +0 -350
  190. package/dist/data-structures/trie/trie.js +0 -610
  191. package/dist/index.d.ts +0 -12
  192. package/dist/index.js +0 -28
  193. package/dist/interfaces/binary-tree.js +0 -2
  194. package/dist/interfaces/doubly-linked-list.js +0 -2
  195. package/dist/interfaces/graph.js +0 -2
  196. package/dist/interfaces/heap.js +0 -2
  197. package/dist/interfaces/index.js +0 -24
  198. package/dist/interfaces/navigator.js +0 -2
  199. package/dist/interfaces/priority-queue.js +0 -2
  200. package/dist/interfaces/segment-tree.js +0 -2
  201. package/dist/interfaces/singly-linked-list.js +0 -2
  202. package/dist/types/common.js +0 -2
  203. package/dist/types/data-structures/base/base.js +0 -2
  204. package/dist/types/data-structures/base/index.js +0 -17
  205. package/dist/types/data-structures/binary-tree/avl-tree-counter.js +0 -2
  206. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.js +0 -2
  207. package/dist/types/data-structures/binary-tree/avl-tree.js +0 -2
  208. package/dist/types/data-structures/binary-tree/binary-indexed-tree.js +0 -2
  209. package/dist/types/data-structures/binary-tree/binary-tree.js +0 -2
  210. package/dist/types/data-structures/binary-tree/bst.js +0 -2
  211. package/dist/types/data-structures/binary-tree/index.js +0 -25
  212. package/dist/types/data-structures/binary-tree/red-black-tree.js +0 -2
  213. package/dist/types/data-structures/binary-tree/segment-tree.js +0 -2
  214. package/dist/types/data-structures/binary-tree/tree-counter.js +0 -2
  215. package/dist/types/data-structures/binary-tree/tree-multi-map.js +0 -2
  216. package/dist/types/data-structures/graph/abstract-graph.js +0 -2
  217. package/dist/types/data-structures/graph/directed-graph.js +0 -2
  218. package/dist/types/data-structures/graph/index.js +0 -19
  219. package/dist/types/data-structures/graph/map-graph.js +0 -2
  220. package/dist/types/data-structures/graph/undirected-graph.js +0 -2
  221. package/dist/types/data-structures/hash/hash-map.js +0 -2
  222. package/dist/types/data-structures/hash/index.js +0 -17
  223. package/dist/types/data-structures/heap/heap.js +0 -2
  224. package/dist/types/data-structures/heap/index.js +0 -17
  225. package/dist/types/data-structures/heap/max-heap.js +0 -2
  226. package/dist/types/data-structures/heap/min-heap.js +0 -2
  227. package/dist/types/data-structures/index.js +0 -28
  228. package/dist/types/data-structures/linked-list/doubly-linked-list.js +0 -2
  229. package/dist/types/data-structures/linked-list/index.js +0 -19
  230. package/dist/types/data-structures/linked-list/singly-linked-list.js +0 -2
  231. package/dist/types/data-structures/linked-list/skip-linked-list.js +0 -2
  232. package/dist/types/data-structures/matrix/index.js +0 -18
  233. package/dist/types/data-structures/matrix/matrix.js +0 -2
  234. package/dist/types/data-structures/matrix/navigator.js +0 -2
  235. package/dist/types/data-structures/priority-queue/index.js +0 -19
  236. package/dist/types/data-structures/priority-queue/max-priority-queue.js +0 -2
  237. package/dist/types/data-structures/priority-queue/min-priority-queue.js +0 -2
  238. package/dist/types/data-structures/priority-queue/priority-queue.js +0 -2
  239. package/dist/types/data-structures/queue/deque.js +0 -2
  240. package/dist/types/data-structures/queue/index.js +0 -18
  241. package/dist/types/data-structures/queue/queue.js +0 -2
  242. package/dist/types/data-structures/stack/index.js +0 -17
  243. package/dist/types/data-structures/stack/stack.js +0 -2
  244. package/dist/types/data-structures/tree/index.js +0 -17
  245. package/dist/types/data-structures/tree/tree.js +0 -2
  246. package/dist/types/data-structures/trie/index.js +0 -17
  247. package/dist/types/data-structures/trie/trie.js +0 -2
  248. package/dist/types/index.js +0 -19
  249. package/dist/types/utils/index.js +0 -18
  250. package/dist/types/utils/utils.js +0 -2
  251. package/dist/types/utils/validate-type.js +0 -2
  252. package/dist/utils/index.d.ts +0 -2
  253. package/dist/utils/index.js +0 -18
  254. package/dist/utils/number.js +0 -24
  255. package/dist/utils/utils.d.ts +0 -209
  256. package/dist/utils/utils.js +0 -353
  257. package/dist/{common → types/common}/index.d.ts +0 -0
  258. package/dist/{constants → types/constants}/index.d.ts +0 -0
  259. package/dist/{data-structures → types/data-structures}/base/iterable-element-base.d.ts +0 -0
  260. package/dist/{data-structures → types/data-structures}/base/iterable-entry-base.d.ts +0 -0
  261. package/dist/{data-structures → types/data-structures}/base/linear-base.d.ts +0 -0
  262. package/dist/{interfaces → types/interfaces}/doubly-linked-list.d.ts +0 -0
  263. package/dist/{interfaces → types/interfaces}/graph.d.ts +0 -0
  264. package/dist/{interfaces → types/interfaces}/heap.d.ts +0 -0
  265. package/dist/{interfaces → types/interfaces}/index.d.ts +0 -0
  266. package/dist/{interfaces → types/interfaces}/navigator.d.ts +0 -0
  267. package/dist/{interfaces → types/interfaces}/priority-queue.d.ts +0 -0
  268. package/dist/{interfaces → types/interfaces}/segment-tree.d.ts +0 -0
  269. package/dist/{interfaces → types/interfaces}/singly-linked-list.d.ts +0 -0
  270. package/dist/types/{common.d.ts → types/common.d.ts} +0 -0
  271. package/dist/types/{data-structures → types/data-structures}/base/base.d.ts +0 -0
  272. package/dist/{data-structures → types/types/data-structures}/index.d.ts +7 -7
  273. package/dist/{data-structures → types/types/data-structures}/linked-list/index.d.ts +0 -0
  274. package/dist/{data-structures → types/types/data-structures}/matrix/index.d.ts +1 -1
  275. /package/dist/{data-structures → types/types/data-structures}/priority-queue/index.d.ts +0 -0
  276. /package/dist/{data-structures → types/types/data-structures}/queue/index.d.ts +0 -0
  277. /package/dist/{data-structures → types/types/data-structures}/stack/index.d.ts +0 -0
  278. /package/dist/{data-structures → types/types/data-structures}/tree/index.d.ts +0 -0
  279. /package/dist/{data-structures → types/types/data-structures}/trie/index.d.ts +0 -0
  280. /package/dist/types/{utils → types/utils}/validate-type.d.ts +0 -0
  281. /package/dist/{utils → types/utils}/number.d.ts +0 -0
@@ -1,754 +0,0 @@
1
- /**
2
- * data-structure-typed
3
- *
4
- * @author Pablo Zeng
5
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
- * @license MIT License
7
- */
8
- import type { BinaryTreeDeleteResult, BinaryTreeOptions, BinaryTreePrintOptions, BTNEntry, DFSOrderPattern, EntryCallback, FamilyPosition, IterationType, NodeCallback, NodeDisplayLayout, NodePredicate, RBTNColor, ToEntryFn } from '../../types';
9
- import { IBinaryTree } from '../../interfaces';
10
- import { IterableEntryBase } from '../base';
11
- import { Range } from '../../common';
12
- /**
13
- * @template K - The type of the key.
14
- * @template V - The type of the value.
15
- */
16
- export declare class BinaryTreeNode<K = any, V = any> {
17
- key: K;
18
- value?: V;
19
- parent?: BinaryTreeNode<K, V>;
20
- /**
21
- * Creates an instance of BinaryTreeNode.
22
- * @remarks Time O(1), Space O(1)
23
- *
24
- * @param key - The key of the node.
25
- * @param [value] - The value associated with the key.
26
- */
27
- constructor(key: K, value?: V);
28
- _left?: BinaryTreeNode<K, V> | null | undefined;
29
- /**
30
- * Gets the left child of the node.
31
- * @remarks Time O(1), Space O(1)
32
- *
33
- * @returns The left child.
34
- */
35
- get left(): BinaryTreeNode<K, V> | null | undefined;
36
- /**
37
- * Sets the left child of the node and updates its parent reference.
38
- * @remarks Time O(1), Space O(1)
39
- *
40
- * @param v - The node to set as the left child.
41
- */
42
- set left(v: BinaryTreeNode<K, V> | null | undefined);
43
- _right?: BinaryTreeNode<K, V> | null | undefined;
44
- /**
45
- * Gets the right child of the node.
46
- * @remarks Time O(1), Space O(1)
47
- *
48
- * @returns The right child.
49
- */
50
- get right(): BinaryTreeNode<K, V> | null | undefined;
51
- /**
52
- * Sets the right child of the node and updates its parent reference.
53
- * @remarks Time O(1), Space O(1)
54
- *
55
- * @param v - The node to set as the right child.
56
- */
57
- set right(v: BinaryTreeNode<K, V> | null | undefined);
58
- _height: number;
59
- /**
60
- * Gets the height of the node (used in self-balancing trees).
61
- * @remarks Time O(1), Space O(1)
62
- *
63
- * @returns The height.
64
- */
65
- get height(): number;
66
- /**
67
- * Sets the height of the node.
68
- * @remarks Time O(1), Space O(1)
69
- *
70
- * @param value - The new height.
71
- */
72
- set height(value: number);
73
- _color: RBTNColor;
74
- /**
75
- * Gets the color of the node (used in Red-Black trees).
76
- * @remarks Time O(1), Space O(1)
77
- *
78
- * @returns The node's color.
79
- */
80
- get color(): RBTNColor;
81
- /**
82
- * Sets the color of the node.
83
- * @remarks Time O(1), Space O(1)
84
- *
85
- * @param value - The new color.
86
- */
87
- set color(value: RBTNColor);
88
- _count: number;
89
- /**
90
- * Gets the count of nodes in the subtree rooted at this node (used in order-statistic trees).
91
- * @remarks Time O(1), Space O(1)
92
- *
93
- * @returns The subtree node count.
94
- */
95
- get count(): number;
96
- /**
97
- * Sets the count of nodes in the subtree.
98
- * @remarks Time O(1), Space O(1)
99
- *
100
- * @param value - The new count.
101
- */
102
- set count(value: number);
103
- /**
104
- * Gets the position of the node relative to its parent.
105
- * @remarks Time O(1), Space O(1)
106
- *
107
- * @returns The family position (e.g., 'ROOT', 'LEFT', 'RIGHT').
108
- */
109
- get familyPosition(): FamilyPosition;
110
- }
111
- /**
112
- * A general Binary Tree implementation.
113
- *
114
- * @remarks
115
- * This class implements a basic Binary Tree, not a Binary Search Tree.
116
- * The `add` operation inserts nodes level-by-level (BFS) into the first available slot.
117
- *
118
- * @template K - The type of the key.
119
- * @template V - The type of the value.
120
- * @template R - The type of the raw data object (if using `toEntryFn`).
121
- * 1. Two Children Maximum: Each node has at most two children.
122
- * 2. Left and Right Children: Nodes have distinct left and right children.
123
- * 3. Depth and Height: Depth is the number of edges from the root to a node; height is the maximum depth in the tree.
124
- * 4. Subtrees: Each child of a node forms the root of a subtree.
125
- * 5. Leaf Nodes: Nodes without children are leaves.
126
- * @example
127
- * // determine loan approval using a decision tree
128
- * // Decision tree structure
129
- * const loanDecisionTree = new BinaryTree<string>(
130
- * ['stableIncome', 'goodCredit', 'Rejected', 'Approved', 'Rejected'],
131
- * { isDuplicate: true }
132
- * );
133
- *
134
- * function determineLoanApproval(
135
- * node?: BinaryTreeNode<string> | null,
136
- * conditions?: { [key: string]: boolean }
137
- * ): string {
138
- * if (!node) throw new Error('Invalid node');
139
- *
140
- * // If it's a leaf node, return the decision result
141
- * if (!node.left && !node.right) return node.key;
142
- *
143
- * // Check if a valid condition exists for the current node's key
144
- * return conditions?.[node.key]
145
- * ? determineLoanApproval(node.left, conditions)
146
- * : determineLoanApproval(node.right, conditions);
147
- * }
148
- *
149
- * // Test case 1: Stable income and good credit score
150
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: true })); // 'Approved'
151
- *
152
- * // Test case 2: Stable income but poor credit score
153
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: true, goodCredit: false })); // 'Rejected'
154
- *
155
- * // Test case 3: No stable income
156
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: true })); // 'Rejected'
157
- *
158
- * // Test case 4: No stable income and poor credit score
159
- * console.log(determineLoanApproval(loanDecisionTree.root, { stableIncome: false, goodCredit: false })); // 'Rejected'
160
- * @example
161
- * // evaluate the arithmetic expression represented by the binary tree
162
- * const expressionTree = new BinaryTree<number | string>(['+', 3, '*', null, null, 5, '-', null, null, 2, 8]);
163
- *
164
- * function evaluate(node?: BinaryTreeNode<number | string> | null): number {
165
- * if (!node) return 0;
166
- *
167
- * if (typeof node.key === 'number') return node.key;
168
- *
169
- * const leftValue = evaluate(node.left); // Evaluate the left subtree
170
- * const rightValue = evaluate(node.right); // Evaluate the right subtree
171
- *
172
- * // Perform the operation based on the current node's operator
173
- * switch (node.key) {
174
- * case '+':
175
- * return leftValue + rightValue;
176
- * case '-':
177
- * return leftValue - rightValue;
178
- * case '*':
179
- * return leftValue * rightValue;
180
- * case '/':
181
- * return rightValue !== 0 ? leftValue / rightValue : 0; // Handle division by zero
182
- * default:
183
- * throw new Error(`Unsupported operator: ${node.key}`);
184
- * }
185
- * }
186
- *
187
- * console.log(evaluate(expressionTree.root)); // -27
188
- */
189
- export declare class BinaryTree<K = any, V = any, R = any> extends IterableEntryBase<K, V | undefined> implements IBinaryTree<K, V, R> {
190
- iterationType: IterationType;
191
- /**
192
- * Creates an instance of BinaryTree.
193
- * @remarks Time O(N * M), where N is the number of items in `keysNodesEntriesOrRaws` and M is the tree size at insertion time (due to O(M) `add` operation). Space O(N) for storing the nodes.
194
- *
195
- * @param [keysNodesEntriesOrRaws=[]] - An iterable of items to add.
196
- * @param [options] - Configuration options for the tree.
197
- */
198
- constructor(keysNodesEntriesOrRaws?: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, options?: BinaryTreeOptions<K, V, R>);
199
- protected _isMapMode: boolean;
200
- /**
201
- * Gets whether the tree is in Map mode.
202
- * @remarks In Map mode (default), values are stored in an external Map, and nodes only hold keys. If false, values are stored directly on the nodes. Time O(1)
203
- *
204
- * @returns True if in Map mode, false otherwise.
205
- */
206
- get isMapMode(): boolean;
207
- protected _isDuplicate: boolean;
208
- /**
209
- * Gets whether the tree allows duplicate keys.
210
- * @remarks Time O(1)
211
- *
212
- * @returns True if duplicates are allowed, false otherwise.
213
- */
214
- get isDuplicate(): boolean;
215
- protected _store: Map<K, V | undefined>;
216
- /**
217
- * Gets the external value store (used in Map mode).
218
- * @remarks Time O(1)
219
- *
220
- * @returns The map storing key-value pairs.
221
- */
222
- get store(): Map<K, V | undefined>;
223
- protected _root?: BinaryTreeNode<K, V> | null | undefined;
224
- /**
225
- * Gets the root node of the tree.
226
- * @remarks Time O(1)
227
- *
228
- * @returns The root node.
229
- */
230
- get root(): BinaryTreeNode<K, V> | null | undefined;
231
- protected _size: number;
232
- /**
233
- * Gets the number of nodes in the tree.
234
- * @remarks Time O(1)
235
- *
236
- * @returns The size of the tree.
237
- */
238
- get size(): number;
239
- protected _NIL: BinaryTreeNode<K, V>;
240
- /**
241
- * Gets the sentinel NIL node (used in self-balancing trees like Red-Black Tree).
242
- * @remarks Time O(1)
243
- *
244
- * @returns The NIL node.
245
- */
246
- get NIL(): BinaryTreeNode<K, V>;
247
- protected _toEntryFn?: ToEntryFn<K, V, R>;
248
- /**
249
- * Gets the function used to convert raw data objects (R) into [key, value] entries.
250
- * @remarks Time O(1)
251
- *
252
- * @returns The conversion function.
253
- */
254
- get toEntryFn(): ToEntryFn<K, V, R> | undefined;
255
- /**
256
- * (Protected) Creates a new node.
257
- * @remarks Time O(1), Space O(1)
258
- *
259
- * @param key - The key for the new node.
260
- * @param [value] - The value for the new node (used if not in Map mode).
261
- * @returns The newly created node.
262
- */
263
- _createNode(key: K, value?: V): BinaryTreeNode<K, V>;
264
- /**
265
- * Creates a new, empty tree of the same type and configuration.
266
- * @remarks Time O(1) (excluding options cloning), Space O(1)
267
- *
268
- * @param [options] - Optional overrides for the new tree's options.
269
- * @returns A new, empty tree instance.
270
- */
271
- createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): this;
272
- /**
273
- * Ensures the input is a node. If it's a key or entry, it searches for the node.
274
- * @remarks Time O(1) if a node is passed. O(N) if a key or entry is passed (due to `getNode` performing a full search). Space O(1) if iterative search, O(H) if recursive (where H is height, O(N) worst-case).
275
- *
276
- * @param keyNodeOrEntry - The item to resolve to a node.
277
- * @param [iterationType=this.iterationType] - The traversal method to use if searching.
278
- * @returns The resolved node, or null/undefined if not found or input is null/undefined.
279
- */
280
- ensureNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined;
281
- /**
282
- * Checks if the given item is a `BinaryTreeNode` instance.
283
- * @remarks Time O(1), Space O(1)
284
- *
285
- * @param keyNodeOrEntry - The item to check.
286
- * @returns True if it's a node, false otherwise.
287
- */
288
- isNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V>;
289
- /**
290
- * Checks if the given item is a raw data object (R) that needs conversion via `toEntryFn`.
291
- * @remarks Time O(1), Space O(1)
292
- *
293
- * @param keyNodeEntryOrRaw - The item to check.
294
- * @returns True if it's a raw object, false otherwise.
295
- */
296
- isRaw(keyNodeEntryOrRaw: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R): keyNodeEntryOrRaw is R;
297
- /**
298
- * Checks if the given item is a "real" node (i.e., not null, undefined, or NIL).
299
- * @remarks Time O(1), Space O(1)
300
- *
301
- * @param keyNodeOrEntry - The item to check.
302
- * @returns True if it's a real node, false otherwise.
303
- */
304
- isRealNode(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V>;
305
- /**
306
- * Checks if the given item is either a "real" node or null.
307
- * @remarks Time O(1), Space O(1)
308
- *
309
- * @param keyNodeOrEntry - The item to check.
310
- * @returns True if it's a real node or null, false otherwise.
311
- */
312
- isRealNodeOrNull(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BinaryTreeNode<K, V> | null;
313
- /**
314
- * Checks if the given item is the sentinel NIL node.
315
- * @remarks Time O(1), Space O(1)
316
- *
317
- * @param keyNodeOrEntry - The item to check.
318
- * @returns True if it's the NIL node, false otherwise.
319
- */
320
- isNIL(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
321
- /**
322
- * Checks if the given item is a `Range` object.
323
- * @remarks Time O(1), Space O(1)
324
- *
325
- * @param keyNodeEntryOrPredicate - The item to check.
326
- * @returns True if it's a Range, false otherwise.
327
- */
328
- isRange(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>> | Range<K>): keyNodeEntryOrPredicate is Range<K>;
329
- /**
330
- * Checks if a node is a leaf (has no real children).
331
- * @remarks Time O(N) if a key/entry is passed (due to `ensureNode`). O(1) if a node is passed. Space O(1) or O(H) (from `ensureNode`).
332
- *
333
- * @param keyNodeOrEntry - The node to check.
334
- * @returns True if the node is a leaf, false otherwise.
335
- */
336
- isLeaf(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
337
- /**
338
- * Checks if the given item is a [key, value] entry pair.
339
- * @remarks Time O(1), Space O(1)
340
- *
341
- * @param keyNodeOrEntry - The item to check.
342
- * @returns True if it's an entry, false otherwise.
343
- */
344
- isEntry(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): keyNodeOrEntry is BTNEntry<K, V>;
345
- /**
346
- * Checks if the given key is valid (comparable or null).
347
- * @remarks Time O(1), Space O(1)
348
- *
349
- * @param key - The key to validate.
350
- * @returns True if the key is valid, false otherwise.
351
- */
352
- isValidKey(key: any): key is K;
353
- /**
354
- * Adds a new node to the tree.
355
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation adds the node at the first available position in a level-order (BFS) traversal. This is NOT a Binary Search Tree insertion. Time O(N), where N is the number of nodes. It must traverse level-by-level to find an empty slot. Space O(N) in the worst case for the BFS queue (e.g., a full last level).
356
- *
357
- * @param keyNodeOrEntry - The key, node, or entry to add.
358
- * @param [value] - The value, if providing just a key.
359
- * @returns True if the addition was successful, false otherwise.
360
- */
361
- add(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): boolean;
362
- /**
363
- * Adds multiple items to the tree.
364
- * @remarks Time O(N * M), where N is the number of items to add and M is the size of the tree at insertion (due to O(M) `add` operation). Space O(M) (from `add`) + O(N) (for the `inserted` array).
365
- *
366
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
367
- * @param [values] - An optional parallel iterable of values.
368
- * @returns An array of booleans indicating the success of each individual `add` operation.
369
- */
370
- addMany(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): boolean[];
371
- /**
372
- * Merges another tree into this one by adding all its nodes.
373
- * @remarks Time O(N * M), same as `addMany`, where N is the size of `anotherTree` and M is the size of this tree. Space O(M) (from `add`).
374
- *
375
- * @param anotherTree - The tree to merge.
376
- */
377
- merge(anotherTree: BinaryTree<K, V, R>): void;
378
- /**
379
- * Clears the tree and refills it with new items.
380
- * @remarks Time O(N) (for `clear`) + O(N * M) (for `addMany`) = O(N * M). Space O(M) (from `addMany`).
381
- *
382
- * @param keysNodesEntriesOrRaws - An iterable of items to add.
383
- * @param [values] - An optional parallel iterable of values.
384
- */
385
- refill(keysNodesEntriesOrRaws: Iterable<K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R>, values?: Iterable<V | undefined>): void;
386
- /**
387
- * Deletes a node from the tree.
388
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). This implementation finds the node, and if it has two children, swaps it with the rightmost node of its left subtree (in-order predecessor) before deleting. Time O(N) in the worst case. O(N) to find the node (`getNode`) and O(H) (which is O(N) worst-case) to find the rightmost node. Space O(1) (if `getNode` is iterative, which it is).
389
- *
390
- * @param keyNodeOrEntry - The node to delete.
391
- * @returns An array containing deletion results (for compatibility with self-balancing trees).
392
- */
393
- delete(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
394
- /**
395
- * Searches the tree for nodes matching a predicate.
396
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Performs a full DFS (pre-order) scan of the tree. Time O(N), as it may visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
397
- *
398
- * @template C - The type of the callback function.
399
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
400
- * @param [onlyOne=false] - If true, stops after finding the first match.
401
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on matching nodes.
402
- * @param [startNode=this._root] - The node to start the search from.
403
- * @param [iterationType=this.iterationType] - Whether to use 'RECURSIVE' or 'ITERATIVE' search.
404
- * @returns An array of results from the callback function for each matching node.
405
- */
406
- search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, onlyOne?: boolean, callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
407
- /**
408
- * Gets all nodes matching a predicate.
409
- * @remarks Time O(N) (via `search`). Space O(H) or O(N) (via `search`).
410
- *
411
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
412
- * @param [onlyOne=false] - If true, stops after finding the first match.
413
- * @param [startNode=this._root] - The node to start the search from.
414
- * @param [iterationType=this.iterationType] - The traversal method.
415
- * @returns An array of matching nodes.
416
- */
417
- getNodes(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V>[];
418
- /**
419
- * Gets the first node matching a predicate.
420
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
421
- *
422
- * @param keyNodeEntryOrPredicate - The key, node, entry, or predicate function to search for.
423
- * @param [startNode=this._root] - The node to start the search from.
424
- * @param [iterationType=this.iterationType] - The traversal method.
425
- * @returns The first matching node, or undefined if not found.
426
- */
427
- getNode(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V> | null>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): BinaryTreeNode<K, V> | null | undefined;
428
- /**
429
- * Gets the value associated with a key.
430
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(1) if in Map mode. O(N) if not in Map mode (uses `getNode`). Space O(1) if in Map mode. O(H) or O(N) otherwise.
431
- *
432
- * @param keyNodeEntryOrPredicate - The key, node, or entry to get the value for.
433
- * @param [startNode=this._root] - The node to start searching from (if not in Map mode).
434
- * @param [iterationType=this.iterationType] - The traversal method (if not in Map mode).
435
- * @returns The associated value, or undefined.
436
- */
437
- get(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): V | undefined;
438
- /**
439
- * Checks if a node matching the predicate exists in the tree.
440
- * @remarks Time O(log N), For BST, Red-Black Tree, and AVL Tree subclasses, the worst-case time is O(log N). Time O(N) in the worst case (via `search`). Space O(H) or O(N) (via `search`).
441
- *
442
- * @param [keyNodeEntryOrPredicate] - The key, node, entry, or predicate to check for.
443
- * @param [startNode] - The node to start the search from.
444
- * @param [iterationType] - The traversal method.
445
- * @returns True if a matching node exists, false otherwise.
446
- */
447
- has(keyNodeEntryOrPredicate?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
448
- /**
449
- * Clears the tree of all nodes and values.
450
- * @remarks Time O(N) if in Map mode (due to `_store.clear()`), O(1) otherwise. Space O(1)
451
- */
452
- clear(): void;
453
- /**
454
- * Checks if the tree is empty.
455
- * @remarks Time O(1), Space O(1)
456
- *
457
- * @returns True if the tree has no nodes, false otherwise.
458
- */
459
- isEmpty(): boolean;
460
- /**
461
- * Checks if the tree is perfectly balanced.
462
- * @remarks A tree is perfectly balanced if the difference between min and max height is at most 1. Time O(N), as it requires two full traversals (`getMinHeight` and `getHeight`). Space O(H) or O(N) (from height calculation).
463
- *
464
- * @param [startNode=this._root] - The node to start checking from.
465
- * @returns True if perfectly balanced, false otherwise.
466
- */
467
- isPerfectlyBalanced(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): boolean;
468
- /**
469
- * Checks if the tree is a valid Binary Search Tree (BST).
470
- * @remarks Time O(N), as it must visit every node. Space O(H) for the call stack (recursive) or explicit stack (iterative), where H is the tree height (O(N) worst-case).
471
- *
472
- * @param [startNode=this._root] - The node to start checking from.
473
- * @param [iterationType=this.iterationType] - The traversal method.
474
- * @returns True if it's a valid BST, false otherwise.
475
- */
476
- isBST(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): boolean;
477
- /**
478
- * Gets the depth of a node (distance from `startNode`).
479
- * @remarks Time O(H), where H is the depth of the `dist` node relative to `startNode`. O(N) worst-case. Space O(1).
480
- *
481
- * @param dist - The node to find the depth of.
482
- * @param [startNode=this._root] - The node to measure depth from (defaults to root).
483
- * @returns The depth (0 if `dist` is `startNode`).
484
- */
485
- getDepth(dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): number;
486
- /**
487
- * Gets the maximum height of the tree (longest path from startNode to a leaf).
488
- * @remarks Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative stack (storing node + depth).
489
- *
490
- * @param [startNode=this._root] - The node to start measuring from.
491
- * @param [iterationType=this.iterationType] - The traversal method.
492
- * @returns The height ( -1 for an empty tree, 0 for a single-node tree).
493
- */
494
- getHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number;
495
- /**
496
- * Gets the minimum height of the tree (shortest path from startNode to a leaf).
497
- * @remarks Time O(N), as it must visit every node. Space O(H) for recursive stack (O(N) worst-case) or O(N) for iterative (due to `depths` Map).
498
- *
499
- * @param [startNode=this._root] - The node to start measuring from.
500
- * @param [iterationType=this.iterationType] - The traversal method.
501
- * @returns The minimum height (-1 for empty, 0 for single node).
502
- */
503
- getMinHeight(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): number;
504
- /**
505
- * Gets the path from a given node up to the root.
506
- * @remarks Time O(H), where H is the depth of the `beginNode`. O(N) worst-case. Space O(H) for the result array.
507
- *
508
- * @template C - The type of the callback function.
509
- * @param beginNode - The node to start the path from.
510
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on each node in the path.
511
- * @param [isReverse=false] - If true, returns the path from root-to-node.
512
- * @returns An array of callback results.
513
- */
514
- getPathToRoot<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, callback?: C, isReverse?: boolean): ReturnType<C>[];
515
- /**
516
- * Finds the leftmost node in a subtree (the node with the smallest key in a BST).
517
- * @remarks Time O(H), where H is the height of the left spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
518
- *
519
- * @template C - The type of the callback function.
520
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on the leftmost node.
521
- * @param [startNode=this._root] - The subtree root to search from.
522
- * @param [iterationType=this.iterationType] - The traversal method.
523
- * @returns The callback result for the leftmost node.
524
- */
525
- getLeftMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
526
- /**
527
- * Finds the rightmost node in a subtree (the node with the largest key in a BST).
528
- * @remarks Time O(H), where H is the height of the right spine. O(N) worst-case. Space O(H) for recursive/trampoline stack.
529
- *
530
- * @template C - The type of the callback function.
531
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - A function to call on the rightmost node.
532
- * @param [startNode=this._root] - The subtree root to search from.
533
- * @param [iterationType=this.iterationType] - The traversal method.
534
- * @returns The callback result for the rightmost node.
535
- */
536
- getRightMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>;
537
- /**
538
- * Gets the Morris traversal predecessor (rightmost node in the left subtree, or node itself).
539
- * @remarks This is primarily a helper for Morris traversal. Time O(H), where H is the height of the left subtree. O(N) worst-case. Space O(1).
540
- *
541
- * @param node - The node to find the predecessor for.
542
- * @returns The Morris predecessor.
543
- */
544
- getPredecessor(node: BinaryTreeNode<K, V>): BinaryTreeNode<K, V>;
545
- /**
546
- * Gets the in-order successor of a node in a BST.
547
- * @remarks Time O(H), where H is the tree height. O(N) worst-case. Space O(H) (due to `getLeftMost` stack).
548
- *
549
- * @param [x] - The node to find the successor of.
550
- * @returns The successor node, or null/undefined if none exists.
551
- */
552
- getSuccessor(x?: K | BinaryTreeNode<K, V> | null): BinaryTreeNode<K, V> | null | undefined;
553
- dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
554
- dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean): ReturnType<C>[];
555
- bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[];
556
- bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[];
557
- /**
558
- * Finds all leaf nodes in the tree.
559
- * @remarks Time O(N), visits every node. Space O(H) for recursive stack or O(N) for iterative queue.
560
- *
561
- * @template C - The type of the callback function.
562
- * @param [callback=this._DEFAULT_NODE_CALLBACK] - Function to call on each leaf node.
563
- * @param [startNode=this._root] - The node to start from.
564
- * @param [iterationType=this.iterationType] - The traversal method.
565
- * @returns An array of callback results.
566
- */
567
- leaves<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType): ReturnType<C>[];
568
- listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: false): ReturnType<C>[][];
569
- listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(callback?: C, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: true): ReturnType<C>[][];
570
- morris<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback?: C, pattern?: DFSOrderPattern, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): ReturnType<C>[];
571
- /**
572
- * Clones the tree.
573
- * @remarks Time O(N * M), where N is the number of nodes and M is the tree size during insertion (due to `bfs` + `add`, and `add` is O(M)). Space O(N) for the new tree and the BFS queue.
574
- *
575
- * @returns A new, cloned instance of the tree.
576
- */
577
- clone(): this;
578
- /**
579
- * Creates a new tree containing only the entries that satisfy the predicate.
580
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion (O(N) iteration + O(M) `add` for each item). Space O(N) for the new tree.
581
- *
582
- * @param predicate - A function to test each [key, value] pair.
583
- * @param [thisArg] - `this` context for the predicate.
584
- * @returns A new, filtered tree.
585
- */
586
- filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
587
- /**
588
- * Creates a new tree by mapping each [key, value] pair to a new entry.
589
- * @remarks Time O(N * M), where N is nodes in this tree, and M is size of the new tree during insertion. Space O(N) for the new tree.
590
- *
591
- * @template MK - New key type.
592
- * @template MV - New value type.
593
- * @template MR - New raw type.
594
- * @param cb - A function to map each [key, value] pair.
595
- * @param [options] - Options for the new tree.
596
- * @param [thisArg] - `this` context for the callback.
597
- * @returns A new, mapped tree.
598
- */
599
- map<MK = K, MV = V, MR = any>(cb: EntryCallback<K, V | undefined, [MK, MV]>, options?: Partial<BinaryTreeOptions<MK, MV, MR>>, thisArg?: unknown): BinaryTree<MK, MV, MR>;
600
- /**
601
- * Generates a string representation of the tree for visualization.
602
- * @remarks Time O(N), visits every node. Space O(N*H) or O(N^2) in the worst case, as the string width can grow significantly.
603
- *
604
- * @param [startNode=this._root] - The node to start printing from.
605
- * @param [options] - Options to control the output (e.g., show nulls).
606
- * @returns The string representation of the tree.
607
- */
608
- toVisual(startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, options?: BinaryTreePrintOptions): string;
609
- /**
610
- * Prints a visual representation of the tree to the console.
611
- * @remarks Time O(N) (via `toVisual`). Space O(N*H) or O(N^2) (via `toVisual`).
612
- *
613
- * @param [options] - Options to control the output.
614
- * @param [startNode=this._root] - The node to start printing from.
615
- */
616
- print(options?: BinaryTreePrintOptions, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): void;
617
- protected _dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(callback: C, pattern?: DFSOrderPattern, onlyOne?: boolean, startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, iterationType?: IterationType, includeNull?: boolean, shouldVisitLeft?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRight?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldVisitRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean, shouldProcessRoot?: (node: BinaryTreeNode<K, V> | null | undefined) => boolean): ReturnType<C>[];
618
- /**
619
- * (Protected) Gets the iterator for the tree (default in-order).
620
- * @remarks Time O(N) for full iteration. O(H) to get the first element. Space O(H) for the iterative stack. O(H) for recursive stack.
621
- *
622
- * @param [node=this._root] - The node to start iteration from.
623
- * @returns An iterator for [key, value] pairs.
624
- */
625
- protected _getIterator(node?: BinaryTreeNode<K, V> | null | undefined): IterableIterator<[K, V | undefined]>;
626
- /**
627
- * (Protected) Default callback function, returns the node's key.
628
- * @remarks Time O(1)
629
- *
630
- * @param node - The node.
631
- * @returns The node's key or undefined.
632
- */
633
- protected _DEFAULT_NODE_CALLBACK: (node: BinaryTreeNode<K, V> | null | undefined) => K | undefined;
634
- /**
635
- * (Protected) Snapshots the current tree's configuration options.
636
- * @remarks Time O(1)
637
- *
638
- * @template TK, TV, TR - Generic types for the options.
639
- * @returns The options object.
640
- */
641
- protected _snapshotOptions<TK = K, TV = V, TR = R>(): BinaryTreeOptions<TK, TV, TR>;
642
- /**
643
- * (Protected) Creates a new, empty instance of the same tree constructor.
644
- * @remarks Time O(1)
645
- *
646
- * @template TK, TV, TR - Generic types for the new instance.
647
- * @param [options] - Options for the new tree.
648
- * @returns A new, empty tree.
649
- */
650
- protected _createInstance<TK = K, TV = V, TR = R>(options?: Partial<BinaryTreeOptions<TK, TV, TR>>): this;
651
- /**
652
- * (Protected) Creates a new instance of the same tree constructor, potentially with different generic types.
653
- * @remarks Time O(N) (or as per constructor) due to processing the iterable.
654
- *
655
- * @template TK, TV, TR - Generic types for the new instance.
656
- * @param [iter=[]] - An iterable to populate the new tree.
657
- * @param [options] - Options for the new tree.
658
- * @returns A new tree.
659
- */
660
- protected _createLike<TK = K, TV = V, TR = R>(iter?: Iterable<TK | BinaryTreeNode<TK, TV> | [TK | null | undefined, TV | undefined] | null | undefined | TR>, options?: Partial<BinaryTreeOptions<TK, TV, TR>>): BinaryTree<TK, TV, TR>;
661
- /**
662
- * (Protected) Converts a key, node, or entry into a standardized [node, value] tuple.
663
- * @remarks Time O(1)
664
- *
665
- * @param keyNodeOrEntry - The input item.
666
- * @param [value] - An optional value (used if input is just a key).
667
- * @returns A tuple of [node, value].
668
- */
669
- protected _keyValueNodeOrEntryToNodeAndValue(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, value?: V): [BinaryTreeNode<K, V> | null | undefined, V | undefined];
670
- /**
671
- * (Protected) Helper for cloning. Performs a BFS and adds all nodes to the new tree.
672
- * @remarks Time O(N * M) (O(N) BFS + O(M) `add` for each node).
673
- *
674
- * @param cloned - The new, empty tree instance to populate.
675
- */
676
- protected _clone(cloned: BinaryTree<K, V, R>): void;
677
- /**
678
- * (Protected) Recursive helper for `toVisual`.
679
- * @remarks Time O(N), Space O(N*H) or O(N^2)
680
- *
681
- * @param node - The current node.
682
- * @param options - Print options.
683
- * @returns Layout information for this subtree.
684
- */
685
- protected _displayAux(node: BinaryTreeNode<K, V> | null | undefined, options: BinaryTreePrintOptions): NodeDisplayLayout;
686
- /**
687
- * (Protected) Swaps the key/value properties of two nodes.
688
- * @remarks Time O(1)
689
- *
690
- * @param srcNode - The source node.
691
- * @param destNode - The destination node.
692
- * @returns The `destNode` (now holding `srcNode`'s properties).
693
- */
694
- protected _swapProperties(srcNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined, destNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): BinaryTreeNode<K, V> | undefined;
695
- /**
696
- * (Protected) Replaces a node in the tree with a new node, maintaining children and parent links.
697
- * @remarks Time O(1)
698
- *
699
- * @param oldNode - The node to be replaced.
700
- * @param newNode - The node to insert.
701
- * @returns The `newNode`.
702
- */
703
- protected _replaceNode(oldNode: BinaryTreeNode<K, V>, newNode: BinaryTreeNode<K, V>): BinaryTreeNode<K, V>;
704
- /**
705
- * (Protected) Sets the root node and clears its parent reference.
706
- * @remarks Time O(1)
707
- *
708
- * @param v - The node to set as root.
709
- */
710
- protected _setRoot(v: BinaryTreeNode<K, V> | null | undefined): void;
711
- /**
712
- * (Protected) Converts a key, node, entry, or predicate into a standardized predicate function.
713
- * @remarks Time O(1)
714
- *
715
- * @param keyNodeEntryOrPredicate - The item to convert.
716
- * @returns A predicate function.
717
- */
718
- protected _ensurePredicate(keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | NodePredicate<BinaryTreeNode<K, V>>): NodePredicate<BinaryTreeNode<K, V>>;
719
- /**
720
- * (Protected) Checks if an item is a predicate function.
721
- * @remarks Time O(1)
722
- *
723
- * @param p - The item to check.
724
- * @returns True if it's a function.
725
- */
726
- protected _isPredicate(p: any): p is NodePredicate<BinaryTreeNode<K, V>>;
727
- /**
728
- * (Protected) Extracts the key from a key, node, or entry.
729
- * @remarks Time O(1)
730
- *
731
- * @param keyNodeOrEntry - The item.
732
- * @returns The extracted key.
733
- */
734
- protected _extractKey(keyNodeOrEntry: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined): K | null | undefined;
735
- /**
736
- * (Protected) Sets a value in the external store (Map mode).
737
- * @remarks Time O(1) (average for Map.set).
738
- *
739
- * @param key - The key.
740
- * @param value - The value.
741
- * @returns True if successful.
742
- */
743
- protected _setValue(key: K | null | undefined, value: V | undefined): false | Map<K, V | undefined>;
744
- /**
745
- * (Protected) Clears all nodes from the tree.
746
- * @remarks Time O(1)
747
- */
748
- protected _clearNodes(): void;
749
- /**
750
- * (Protected) Clears all values from the external store.
751
- * @remarks Time O(N)
752
- */
753
- protected _clearValues(): void;
754
- }