data-structure-typed 2.0.5 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (260) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/COMMANDS.md +17 -0
  3. package/benchmark/report.html +13 -77
  4. package/benchmark/report.json +145 -177
  5. package/dist/cjs/data-structures/base/iterable-element-base.d.ts +186 -83
  6. package/dist/cjs/data-structures/base/iterable-element-base.js +149 -107
  7. package/dist/cjs/data-structures/base/iterable-element-base.js.map +1 -1
  8. package/dist/cjs/data-structures/base/iterable-entry-base.d.ts +95 -119
  9. package/dist/cjs/data-structures/base/iterable-entry-base.js +59 -116
  10. package/dist/cjs/data-structures/base/iterable-entry-base.js.map +1 -1
  11. package/dist/cjs/data-structures/base/linear-base.d.ts +250 -192
  12. package/dist/cjs/data-structures/base/linear-base.js +137 -274
  13. package/dist/cjs/data-structures/base/linear-base.js.map +1 -1
  14. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  15. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js +171 -205
  16. package/dist/cjs/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  17. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  18. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js +135 -87
  19. package/dist/cjs/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  20. package/dist/cjs/data-structures/binary-tree/avl-tree.d.ts +138 -149
  21. package/dist/cjs/data-structures/binary-tree/avl-tree.js +208 -195
  22. package/dist/cjs/data-structures/binary-tree/avl-tree.js.map +1 -1
  23. package/dist/cjs/data-structures/binary-tree/binary-tree.d.ts +476 -632
  24. package/dist/cjs/data-structures/binary-tree/binary-tree.js +594 -865
  25. package/dist/cjs/data-structures/binary-tree/binary-tree.js.map +1 -1
  26. package/dist/cjs/data-structures/binary-tree/bst.d.ts +258 -306
  27. package/dist/cjs/data-structures/binary-tree/bst.js +505 -481
  28. package/dist/cjs/data-structures/binary-tree/bst.js.map +1 -1
  29. package/dist/cjs/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  30. package/dist/cjs/data-structures/binary-tree/red-black-tree.js +114 -209
  31. package/dist/cjs/data-structures/binary-tree/red-black-tree.js.map +1 -1
  32. package/dist/cjs/data-structures/binary-tree/tree-counter.d.ts +132 -154
  33. package/dist/cjs/data-structures/binary-tree/tree-counter.js +172 -203
  34. package/dist/cjs/data-structures/binary-tree/tree-counter.js.map +1 -1
  35. package/dist/cjs/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  36. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js +105 -85
  37. package/dist/cjs/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  38. package/dist/cjs/data-structures/graph/abstract-graph.d.ts +238 -233
  39. package/dist/cjs/data-structures/graph/abstract-graph.js +267 -237
  40. package/dist/cjs/data-structures/graph/abstract-graph.js.map +1 -1
  41. package/dist/cjs/data-structures/graph/directed-graph.d.ts +108 -224
  42. package/dist/cjs/data-structures/graph/directed-graph.js +146 -233
  43. package/dist/cjs/data-structures/graph/directed-graph.js.map +1 -1
  44. package/dist/cjs/data-structures/graph/map-graph.d.ts +49 -55
  45. package/dist/cjs/data-structures/graph/map-graph.js +56 -59
  46. package/dist/cjs/data-structures/graph/map-graph.js.map +1 -1
  47. package/dist/cjs/data-structures/graph/undirected-graph.d.ts +103 -146
  48. package/dist/cjs/data-structures/graph/undirected-graph.js +129 -149
  49. package/dist/cjs/data-structures/graph/undirected-graph.js.map +1 -1
  50. package/dist/cjs/data-structures/hash/hash-map.d.ts +164 -338
  51. package/dist/cjs/data-structures/hash/hash-map.js +270 -457
  52. package/dist/cjs/data-structures/hash/hash-map.js.map +1 -1
  53. package/dist/cjs/data-structures/heap/heap.d.ts +214 -289
  54. package/dist/cjs/data-structures/heap/heap.js +340 -349
  55. package/dist/cjs/data-structures/heap/heap.js.map +1 -1
  56. package/dist/cjs/data-structures/heap/max-heap.d.ts +11 -47
  57. package/dist/cjs/data-structures/heap/max-heap.js +11 -66
  58. package/dist/cjs/data-structures/heap/max-heap.js.map +1 -1
  59. package/dist/cjs/data-structures/heap/min-heap.d.ts +12 -47
  60. package/dist/cjs/data-structures/heap/min-heap.js +11 -66
  61. package/dist/cjs/data-structures/heap/min-heap.js.map +1 -1
  62. package/dist/cjs/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  63. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js +368 -494
  64. package/dist/cjs/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  65. package/dist/cjs/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  66. package/dist/cjs/data-structures/linked-list/singly-linked-list.js +447 -466
  67. package/dist/cjs/data-structures/linked-list/singly-linked-list.js.map +1 -1
  68. package/dist/cjs/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  69. package/dist/cjs/data-structures/linked-list/skip-linked-list.js +0 -100
  70. package/dist/cjs/data-structures/linked-list/skip-linked-list.js.map +1 -1
  71. package/dist/cjs/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  72. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js +11 -78
  73. package/dist/cjs/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  74. package/dist/cjs/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  75. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js +10 -79
  76. package/dist/cjs/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  77. package/dist/cjs/data-structures/priority-queue/priority-queue.d.ts +2 -61
  78. package/dist/cjs/data-structures/priority-queue/priority-queue.js +8 -83
  79. package/dist/cjs/data-structures/priority-queue/priority-queue.js.map +1 -1
  80. package/dist/cjs/data-structures/queue/deque.d.ts +227 -254
  81. package/dist/cjs/data-structures/queue/deque.js +309 -348
  82. package/dist/cjs/data-structures/queue/deque.js.map +1 -1
  83. package/dist/cjs/data-structures/queue/queue.d.ts +180 -201
  84. package/dist/cjs/data-structures/queue/queue.js +265 -248
  85. package/dist/cjs/data-structures/queue/queue.js.map +1 -1
  86. package/dist/cjs/data-structures/stack/stack.d.ts +124 -102
  87. package/dist/cjs/data-structures/stack/stack.js +181 -125
  88. package/dist/cjs/data-structures/stack/stack.js.map +1 -1
  89. package/dist/cjs/data-structures/trie/trie.d.ts +164 -165
  90. package/dist/cjs/data-structures/trie/trie.js +189 -172
  91. package/dist/cjs/data-structures/trie/trie.js.map +1 -1
  92. package/dist/cjs/interfaces/binary-tree.d.ts +56 -6
  93. package/dist/cjs/interfaces/graph.d.ts +16 -0
  94. package/dist/cjs/types/data-structures/base/base.d.ts +1 -1
  95. package/dist/cjs/types/data-structures/graph/abstract-graph.d.ts +4 -0
  96. package/dist/cjs/types/utils/utils.d.ts +1 -0
  97. package/dist/cjs/utils/utils.d.ts +1 -1
  98. package/dist/cjs/utils/utils.js +2 -1
  99. package/dist/cjs/utils/utils.js.map +1 -1
  100. package/dist/esm/data-structures/base/iterable-element-base.d.ts +186 -83
  101. package/dist/esm/data-structures/base/iterable-element-base.js +155 -107
  102. package/dist/esm/data-structures/base/iterable-element-base.js.map +1 -1
  103. package/dist/esm/data-structures/base/iterable-entry-base.d.ts +95 -119
  104. package/dist/esm/data-structures/base/iterable-entry-base.js +59 -116
  105. package/dist/esm/data-structures/base/iterable-entry-base.js.map +1 -1
  106. package/dist/esm/data-structures/base/linear-base.d.ts +250 -192
  107. package/dist/esm/data-structures/base/linear-base.js +137 -274
  108. package/dist/esm/data-structures/base/linear-base.js.map +1 -1
  109. package/dist/esm/data-structures/binary-tree/avl-tree-counter.d.ts +126 -158
  110. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js +171 -212
  111. package/dist/esm/data-structures/binary-tree/avl-tree-counter.js.map +1 -1
  112. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.d.ts +100 -69
  113. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js +133 -94
  114. package/dist/esm/data-structures/binary-tree/avl-tree-multi-map.js.map +1 -1
  115. package/dist/esm/data-structures/binary-tree/avl-tree.d.ts +138 -149
  116. package/dist/esm/data-structures/binary-tree/avl-tree.js +206 -200
  117. package/dist/esm/data-structures/binary-tree/avl-tree.js.map +1 -1
  118. package/dist/esm/data-structures/binary-tree/binary-tree.d.ts +476 -632
  119. package/dist/esm/data-structures/binary-tree/binary-tree.js +598 -874
  120. package/dist/esm/data-structures/binary-tree/binary-tree.js.map +1 -1
  121. package/dist/esm/data-structures/binary-tree/bst.d.ts +258 -306
  122. package/dist/esm/data-structures/binary-tree/bst.js +507 -487
  123. package/dist/esm/data-structures/binary-tree/bst.js.map +1 -1
  124. package/dist/esm/data-structures/binary-tree/red-black-tree.d.ts +107 -179
  125. package/dist/esm/data-structures/binary-tree/red-black-tree.js +114 -215
  126. package/dist/esm/data-structures/binary-tree/red-black-tree.js.map +1 -1
  127. package/dist/esm/data-structures/binary-tree/tree-counter.d.ts +132 -154
  128. package/dist/esm/data-structures/binary-tree/tree-counter.js +175 -209
  129. package/dist/esm/data-structures/binary-tree/tree-counter.js.map +1 -1
  130. package/dist/esm/data-structures/binary-tree/tree-multi-map.d.ts +72 -69
  131. package/dist/esm/data-structures/binary-tree/tree-multi-map.js +103 -92
  132. package/dist/esm/data-structures/binary-tree/tree-multi-map.js.map +1 -1
  133. package/dist/esm/data-structures/graph/abstract-graph.d.ts +238 -233
  134. package/dist/esm/data-structures/graph/abstract-graph.js +267 -237
  135. package/dist/esm/data-structures/graph/abstract-graph.js.map +1 -1
  136. package/dist/esm/data-structures/graph/directed-graph.d.ts +108 -224
  137. package/dist/esm/data-structures/graph/directed-graph.js +145 -233
  138. package/dist/esm/data-structures/graph/directed-graph.js.map +1 -1
  139. package/dist/esm/data-structures/graph/map-graph.d.ts +49 -55
  140. package/dist/esm/data-structures/graph/map-graph.js +56 -59
  141. package/dist/esm/data-structures/graph/map-graph.js.map +1 -1
  142. package/dist/esm/data-structures/graph/undirected-graph.d.ts +103 -146
  143. package/dist/esm/data-structures/graph/undirected-graph.js +128 -149
  144. package/dist/esm/data-structures/graph/undirected-graph.js.map +1 -1
  145. package/dist/esm/data-structures/hash/hash-map.d.ts +164 -338
  146. package/dist/esm/data-structures/hash/hash-map.js +270 -457
  147. package/dist/esm/data-structures/hash/hash-map.js.map +1 -1
  148. package/dist/esm/data-structures/heap/heap.d.ts +214 -289
  149. package/dist/esm/data-structures/heap/heap.js +329 -349
  150. package/dist/esm/data-structures/heap/heap.js.map +1 -1
  151. package/dist/esm/data-structures/heap/max-heap.d.ts +11 -47
  152. package/dist/esm/data-structures/heap/max-heap.js +11 -66
  153. package/dist/esm/data-structures/heap/max-heap.js.map +1 -1
  154. package/dist/esm/data-structures/heap/min-heap.d.ts +12 -47
  155. package/dist/esm/data-structures/heap/min-heap.js +11 -66
  156. package/dist/esm/data-structures/heap/min-heap.js.map +1 -1
  157. package/dist/esm/data-structures/linked-list/doubly-linked-list.d.ts +231 -347
  158. package/dist/esm/data-structures/linked-list/doubly-linked-list.js +368 -495
  159. package/dist/esm/data-structures/linked-list/doubly-linked-list.js.map +1 -1
  160. package/dist/esm/data-structures/linked-list/singly-linked-list.d.ts +261 -310
  161. package/dist/esm/data-structures/linked-list/singly-linked-list.js +448 -467
  162. package/dist/esm/data-structures/linked-list/singly-linked-list.js.map +1 -1
  163. package/dist/esm/data-structures/linked-list/skip-linked-list.d.ts +0 -107
  164. package/dist/esm/data-structures/linked-list/skip-linked-list.js +0 -100
  165. package/dist/esm/data-structures/linked-list/skip-linked-list.js.map +1 -1
  166. package/dist/esm/data-structures/priority-queue/max-priority-queue.d.ts +12 -56
  167. package/dist/esm/data-structures/priority-queue/max-priority-queue.js +11 -78
  168. package/dist/esm/data-structures/priority-queue/max-priority-queue.js.map +1 -1
  169. package/dist/esm/data-structures/priority-queue/min-priority-queue.d.ts +11 -57
  170. package/dist/esm/data-structures/priority-queue/min-priority-queue.js +10 -79
  171. package/dist/esm/data-structures/priority-queue/min-priority-queue.js.map +1 -1
  172. package/dist/esm/data-structures/priority-queue/priority-queue.d.ts +2 -61
  173. package/dist/esm/data-structures/priority-queue/priority-queue.js +8 -83
  174. package/dist/esm/data-structures/priority-queue/priority-queue.js.map +1 -1
  175. package/dist/esm/data-structures/queue/deque.d.ts +227 -254
  176. package/dist/esm/data-structures/queue/deque.js +313 -348
  177. package/dist/esm/data-structures/queue/deque.js.map +1 -1
  178. package/dist/esm/data-structures/queue/queue.d.ts +180 -201
  179. package/dist/esm/data-structures/queue/queue.js +263 -248
  180. package/dist/esm/data-structures/queue/queue.js.map +1 -1
  181. package/dist/esm/data-structures/stack/stack.d.ts +124 -102
  182. package/dist/esm/data-structures/stack/stack.js +181 -125
  183. package/dist/esm/data-structures/stack/stack.js.map +1 -1
  184. package/dist/esm/data-structures/trie/trie.d.ts +164 -165
  185. package/dist/esm/data-structures/trie/trie.js +193 -172
  186. package/dist/esm/data-structures/trie/trie.js.map +1 -1
  187. package/dist/esm/interfaces/binary-tree.d.ts +56 -6
  188. package/dist/esm/interfaces/graph.d.ts +16 -0
  189. package/dist/esm/types/data-structures/base/base.d.ts +1 -1
  190. package/dist/esm/types/data-structures/graph/abstract-graph.d.ts +4 -0
  191. package/dist/esm/types/utils/utils.d.ts +1 -0
  192. package/dist/esm/utils/utils.d.ts +1 -1
  193. package/dist/esm/utils/utils.js +2 -1
  194. package/dist/esm/utils/utils.js.map +1 -1
  195. package/dist/umd/data-structure-typed.js +4685 -6477
  196. package/dist/umd/data-structure-typed.min.js +8 -6
  197. package/dist/umd/data-structure-typed.min.js.map +1 -1
  198. package/package.json +3 -4
  199. package/src/data-structures/base/iterable-element-base.ts +238 -115
  200. package/src/data-structures/base/iterable-entry-base.ts +96 -120
  201. package/src/data-structures/base/linear-base.ts +271 -277
  202. package/src/data-structures/binary-tree/avl-tree-counter.ts +198 -216
  203. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +192 -101
  204. package/src/data-structures/binary-tree/avl-tree.ts +239 -206
  205. package/src/data-structures/binary-tree/binary-tree.ts +660 -889
  206. package/src/data-structures/binary-tree/bst.ts +568 -570
  207. package/src/data-structures/binary-tree/red-black-tree.ts +161 -222
  208. package/src/data-structures/binary-tree/tree-counter.ts +199 -218
  209. package/src/data-structures/binary-tree/tree-multi-map.ts +131 -97
  210. package/src/data-structures/graph/abstract-graph.ts +339 -264
  211. package/src/data-structures/graph/directed-graph.ts +146 -236
  212. package/src/data-structures/graph/map-graph.ts +63 -60
  213. package/src/data-structures/graph/undirected-graph.ts +129 -152
  214. package/src/data-structures/hash/hash-map.ts +274 -496
  215. package/src/data-structures/heap/heap.ts +389 -402
  216. package/src/data-structures/heap/max-heap.ts +12 -76
  217. package/src/data-structures/heap/min-heap.ts +13 -76
  218. package/src/data-structures/linked-list/doubly-linked-list.ts +426 -530
  219. package/src/data-structures/linked-list/singly-linked-list.ts +495 -517
  220. package/src/data-structures/linked-list/skip-linked-list.ts +1 -108
  221. package/src/data-structures/priority-queue/max-priority-queue.ts +12 -87
  222. package/src/data-structures/priority-queue/min-priority-queue.ts +11 -88
  223. package/src/data-structures/priority-queue/priority-queue.ts +3 -92
  224. package/src/data-structures/queue/deque.ts +381 -357
  225. package/src/data-structures/queue/queue.ts +310 -264
  226. package/src/data-structures/stack/stack.ts +217 -131
  227. package/src/data-structures/trie/trie.ts +240 -175
  228. package/src/interfaces/binary-tree.ts +240 -6
  229. package/src/interfaces/graph.ts +37 -0
  230. package/src/types/data-structures/base/base.ts +5 -5
  231. package/src/types/data-structures/graph/abstract-graph.ts +5 -0
  232. package/src/types/utils/utils.ts +2 -0
  233. package/src/utils/utils.ts +9 -14
  234. package/test/integration/index.html +1 -1
  235. package/test/performance/benchmark-runner.ts +528 -0
  236. package/test/performance/reportor.mjs +43 -43
  237. package/test/performance/runner-config.json +39 -0
  238. package/test/performance/single-suite-runner.ts +69 -0
  239. package/test/unit/data-structures/binary-tree/avl-tree-counter.test.ts +3 -3
  240. package/test/unit/data-structures/binary-tree/avl-tree-multi-map.test.ts +5 -5
  241. package/test/unit/data-structures/binary-tree/avl-tree.test.ts +4 -4
  242. package/test/unit/data-structures/binary-tree/binary-tree.test.ts +350 -90
  243. package/test/unit/data-structures/binary-tree/bst.test.ts +12 -9
  244. package/test/unit/data-structures/binary-tree/red-black-tree.test.ts +2 -2
  245. package/test/unit/data-structures/binary-tree/tree-counter.test.ts +25 -24
  246. package/test/unit/data-structures/binary-tree/tree-multi-map.test.ts +3 -3
  247. package/test/unit/data-structures/graph/abstract-graph.test.ts +0 -4
  248. package/test/unit/data-structures/graph/directed-graph.test.ts +1 -1
  249. package/test/unit/data-structures/heap/heap.test.ts +14 -21
  250. package/test/unit/data-structures/heap/max-heap.test.ts +5 -9
  251. package/test/unit/data-structures/heap/min-heap.test.ts +1 -4
  252. package/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +14 -14
  253. package/test/unit/data-structures/linked-list/singly-linked-list.test.ts +0 -7
  254. package/test/unit/data-structures/priority-queue/max-priority-queue.test.ts +8 -11
  255. package/test/unit/data-structures/priority-queue/min-priority-queue.test.ts +1 -4
  256. package/test/unit/data-structures/priority-queue/priority-queue.test.ts +1 -4
  257. package/test/unit/data-structures/queue/queue.test.ts +4 -5
  258. package/test/unit/utils/utils.test.ts +0 -1
  259. package/test/performance/data-structures/binary-tree/avl-tree.test.mjs +0 -71
  260. package/test/performance/data-structures/binary-tree/red-black-tree.test.mjs +0 -81
@@ -1,16 +1,250 @@
1
1
  import { BinaryTreeNode } from '../data-structures';
2
- import type { BinaryTreeDeleteResult, BinaryTreeOptions, BTNRep, NodePredicate } from '../types';
2
+ import type {
3
+ BinaryTreeDeleteResult,
4
+ BinaryTreeOptions,
5
+ BTNRep,
6
+ DFSOrderPattern,
7
+ EntryCallback,
8
+ IterationType,
9
+ NodeCallback,
10
+ NodePredicate,
11
+ OptNodeOrNull,
12
+ ReduceEntryCallback,
13
+ ToEntryFn
14
+ } from '../types';
3
15
 
4
- export interface IBinaryTree<K = any, V = any, R = object, MK = any, MV = any, MR = object> {
5
- createNode(key: K, value?: BinaryTreeNode['value']): BinaryTreeNode;
16
+ /**
17
+ * Public, implementation-agnostic binary tree API.
18
+ * K = key, V = value, R = raw/record used with toEntryFn (optional).
19
+ * Transforming methods like `map` use method-level generics MK/MV/MR.
20
+ */
21
+ export interface IBinaryTree<K = any, V = any, R extends object = object> {
22
+ // ---- state ----
23
+ readonly size: number;
24
+ readonly root: BinaryTreeNode<K, V> | null | undefined;
25
+ readonly isMapMode: boolean;
26
+ // NOTE: iterationType is mutable on the class; remove readonly here to match
27
+ iterationType: IterationType;
28
+ // Extra public state/getters implemented by BinaryTree (useful to callers)
29
+ readonly NIL: BinaryTreeNode<K, V>;
30
+ readonly store: Map<K, V | undefined>;
31
+ readonly toEntryFn?: ToEntryFn<K, V, R>;
32
+ readonly isDuplicate: boolean;
6
33
 
7
- createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R, MK, MV, MR>;
34
+ // ---- construction / mutation ----
35
+ _createNode(key: K, value?: BinaryTreeNode<K, V>['value']): BinaryTreeNode<K, V>;
36
+
37
+ createTree(options?: Partial<BinaryTreeOptions<K, V, R>>): IBinaryTree<K, V, R>;
8
38
 
9
39
  add(keyOrNodeOrEntryOrRawElement: BTNRep<K, V, BinaryTreeNode<K, V>>, value?: V, count?: number): boolean;
10
40
 
11
- addMany(nodes: Iterable<BTNRep<K, V, BinaryTreeNode<K, V>>>, values?: Iterable<V | undefined>): boolean[];
41
+ // Accept raw R as well (when toEntryFn is configured)
42
+ addMany(
43
+ keysNodesEntriesOrRaws: Iterable<
44
+ K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
45
+ >,
46
+ values?: Iterable<V | undefined>
47
+ ): boolean[];
12
48
 
49
+ // Accept BTNRep, predicate, or raw R for deletion
13
50
  delete(
14
- predicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V>>
51
+ keyNodeEntryRawOrPredicate: R | BTNRep<K, V, BinaryTreeNode<K, V>> | NodePredicate<BinaryTreeNode<K, V> | null>
15
52
  ): BinaryTreeDeleteResult<BinaryTreeNode<K, V>>[];
53
+
54
+ clear(): void;
55
+
56
+ isEmpty(): boolean;
57
+
58
+ // ---- query / read ----
59
+
60
+ // Widen `get` to support entry and optional traversal context (matches impl)
61
+ get(
62
+ keyNodeEntryOrPredicate: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
63
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
64
+ iterationType?: IterationType
65
+ ): V | undefined;
66
+
67
+ // `has` also supports node/entry/predicate and optional traversal context
68
+ has(
69
+ keyNodeEntryOrPredicate?:
70
+ | K
71
+ | BinaryTreeNode<K, V>
72
+ | [K | null | undefined, V | undefined]
73
+ | null
74
+ | undefined
75
+ | NodePredicate<BinaryTreeNode<K, V> | null>,
76
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
77
+ iterationType?: IterationType
78
+ ): boolean;
79
+
80
+ hasValue(value: V): boolean;
81
+
82
+ find(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): [K, V | undefined] | undefined;
83
+
84
+ // ---- iteration ----
85
+ [Symbol.iterator](): IterableIterator<[K, V | undefined]>;
86
+
87
+ entries(): IterableIterator<[K, V | undefined]>;
88
+
89
+ keys(): IterableIterator<K>;
90
+
91
+ values(): IterableIterator<V | undefined>;
92
+
93
+ forEach(callbackfn: EntryCallback<K, V | undefined, void>, thisArg?: unknown): void;
94
+
95
+ every(callbackfn: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): boolean;
96
+
97
+ some(callbackfn: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): boolean;
98
+
99
+ reduce<U>(reducer: ReduceEntryCallback<K, V | undefined, U>, initialValue: U): U;
100
+
101
+ // ---- node access / extremes ----
102
+ getNode(
103
+ keyNodeEntryOrPredicate:
104
+ | K
105
+ | BinaryTreeNode<K, V>
106
+ | [K | null | undefined, V | undefined]
107
+ | null
108
+ | undefined
109
+ | NodePredicate<BinaryTreeNode<K, V> | null>,
110
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
111
+ iterationType?: IterationType
112
+ ): OptNodeOrNull<BinaryTreeNode<K, V>>;
113
+
114
+ getLeftMost<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(
115
+ callback?: C,
116
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
117
+ iterationType?: IterationType
118
+ ): ReturnType<C>;
119
+
120
+ getRightMost<C extends NodeCallback<BinaryTreeNode<K, V> | undefined>>(
121
+ callback?: C,
122
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
123
+ iterationType?: IterationType
124
+ ): ReturnType<C>;
125
+
126
+ // ---- traversal ----
127
+ dfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(
128
+ callback?: C,
129
+ pattern?: DFSOrderPattern,
130
+ onlyOne?: boolean,
131
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
132
+ iterationType?: IterationType
133
+ ): ReturnType<C>[];
134
+
135
+ dfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
136
+ callback?: C,
137
+ pattern?: DFSOrderPattern,
138
+ onlyOne?: boolean,
139
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
140
+ iterationType?: IterationType,
141
+ includeNull?: boolean
142
+ ): ReturnType<C>[];
143
+
144
+ bfs<C extends NodeCallback<BinaryTreeNode<K, V>>>(
145
+ callback?: C,
146
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
147
+ iterationType?: IterationType,
148
+ includeNull?: false
149
+ ): ReturnType<C>[];
150
+
151
+ bfs<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
152
+ callback?: C,
153
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
154
+ iterationType?: IterationType,
155
+ includeNull?: true
156
+ ): ReturnType<C>[];
157
+
158
+ morris<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
159
+ callback?: C,
160
+ pattern?: DFSOrderPattern,
161
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
162
+ ): ReturnType<C>[];
163
+
164
+ leaves<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
165
+ callback?: C,
166
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
167
+ iterationType?: IterationType
168
+ ): ReturnType<C>[];
169
+
170
+ listLevels<C extends NodeCallback<BinaryTreeNode<K, V>>>(
171
+ callback?: C,
172
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
173
+ iterationType?: IterationType,
174
+ includeNull?: false
175
+ ): ReturnType<C>[][];
176
+
177
+ listLevels<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
178
+ callback?: C,
179
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
180
+ iterationType?: IterationType,
181
+ includeNull?: true
182
+ ): ReturnType<C>[][];
183
+
184
+ getPathToRoot<C extends NodeCallback<OptNodeOrNull<BinaryTreeNode<K, V>>>>(
185
+ beginNode: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
186
+ callback?: C,
187
+ isReverse?: boolean
188
+ ): ReturnType<C>[];
189
+
190
+ // ---- metrics & validation ----
191
+ getDepth(
192
+ dist: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
193
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
194
+ ): number;
195
+
196
+ getHeight(
197
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
198
+ iterationType?: IterationType
199
+ ): number;
200
+
201
+ getMinHeight(
202
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
203
+ iterationType?: IterationType
204
+ ): number;
205
+
206
+ isPerfectlyBalanced(
207
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined
208
+ ): boolean;
209
+
210
+ isBST(
211
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
212
+ iterationType?: IterationType
213
+ ): boolean;
214
+
215
+ // ---- search helpers ----
216
+ search<C extends NodeCallback<BinaryTreeNode<K, V> | null>>(
217
+ keyNodeEntryOrPredicate:
218
+ | K
219
+ | BinaryTreeNode<K, V>
220
+ | [K | null | undefined, V | undefined]
221
+ | null
222
+ | undefined
223
+ | NodePredicate<BinaryTreeNode<K, V> | null>,
224
+ onlyOne?: boolean,
225
+ callback?: C,
226
+ startNode?: K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined,
227
+ iterationType?: IterationType
228
+ ): ReturnType<C>[];
229
+
230
+ // ---- immutable transforms ----
231
+ clone(): this;
232
+
233
+ filter(predicate: EntryCallback<K, V | undefined, boolean>, thisArg?: unknown): this;
234
+
235
+ map<MK = K, MV = V, MR extends object = object>(
236
+ callback: EntryCallback<K, V | undefined, [MK, MV]>,
237
+ options?: Partial<BinaryTreeOptions<MK, MV, MR>>,
238
+ thisArg?: unknown
239
+ ): IBinaryTree<MK, MV, MR>;
240
+
241
+ // ---- bulk / interop ----
242
+ merge(anotherTree: IBinaryTree<K, V, R>): void;
243
+
244
+ refill(
245
+ keysNodesEntriesOrRaws: Iterable<
246
+ K | BinaryTreeNode<K, V> | [K | null | undefined, V | undefined] | null | undefined | R
247
+ >,
248
+ values?: Iterable<V | undefined>
249
+ ): void;
16
250
  }
@@ -1,7 +1,44 @@
1
1
  import { VertexKey } from '../types';
2
2
 
3
3
  export interface IGraph<V, E, VO, EO> {
4
+ // Vertex factories
4
5
  createVertex(key: VertexKey, value?: V): VO;
5
6
 
7
+ // Edge factories
6
8
  createEdge(srcOrV1: VertexKey, destOrV2: VertexKey, weight?: number, value?: E): EO;
9
+
10
+ // Core vertex ops
11
+ getVertex(vertexKey: VertexKey): VO | undefined;
12
+
13
+ hasVertex(vertexOrKey: VO | VertexKey): boolean;
14
+
15
+ addVertex(vertex: VO): boolean;
16
+
17
+ addVertex(key: VertexKey, value?: V): boolean;
18
+
19
+ deleteVertex(vertexOrKey: VO | VertexKey): boolean;
20
+
21
+ // Core edge ops
22
+ deleteEdge(edge: EO): EO | undefined;
23
+
24
+ getEdge(srcOrKey: VO | VertexKey, destOrKey: VO | VertexKey): EO | undefined;
25
+
26
+ degreeOf(vertexOrKey: VO | VertexKey): number;
27
+
28
+ edgeSet(): EO[];
29
+
30
+ edgesOf(vertexOrKey: VO | VertexKey): EO[];
31
+
32
+ getNeighbors(vertexOrKey: VO | VertexKey): VO[];
33
+
34
+ getEndsOfEdge(edge: EO): [VO, VO] | undefined;
35
+
36
+ // Container-like ops
37
+ isEmpty(): boolean;
38
+
39
+ clear(): void;
40
+
41
+ clone(): this;
42
+
43
+ filter(...args: any[]): this;
7
44
  }
@@ -11,12 +11,12 @@ export type ReduceEntryCallback<K, V, R> = (
11
11
  original: IterableEntryBase<K, V>
12
12
  ) => R;
13
13
 
14
- export type ReduceElementCallback<E, R, RT = E> = (
15
- accumulator: RT,
16
- element: E,
14
+ export type ReduceElementCallback<E, R, U = E> = (
15
+ accumulator: U,
16
+ value: E,
17
17
  index: number,
18
- original: IterableElementBase<E, R>
19
- ) => RT;
18
+ self: IterableElementBase<E, R>
19
+ ) => U;
20
20
 
21
21
  export type ReduceLinearCallback<E, RT = E> = (
22
22
  accumulator: RT,
@@ -11,3 +11,8 @@ export type DijkstraResult<V> =
11
11
  minPath: V[];
12
12
  }
13
13
  | undefined;
14
+
15
+ export type GraphOptions<V = any> = {
16
+ vertexValueInitializer?: (key: VertexKey) => V;
17
+ defaultEdgeWeight?: number;
18
+ };
@@ -4,6 +4,8 @@ export type Any = string | number | bigint | boolean | symbol | undefined | obje
4
4
 
5
5
  export type Arithmetic = number | bigint;
6
6
 
7
+ export type ElemOf<T> = T extends (infer U)[] ? U : never;
8
+
7
9
  export type ComparablePrimitive = number | bigint | string | boolean;
8
10
 
9
11
  export interface BaseComparableObject {
@@ -5,7 +5,7 @@
5
5
  * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
6
6
  * @license MIT License
7
7
  */
8
- import type { Comparable, ComparablePrimitive, TrampolineThunk, Trampoline } from '../types';
8
+ import type { Comparable, ComparablePrimitive, Trampoline, TrampolineThunk } from '../types';
9
9
 
10
10
  /**
11
11
  * The function generates a random UUID (Universally Unique Identifier) in TypeScript.
@@ -209,9 +209,7 @@ export function isComparable(value: unknown, isForceObjectComparable = false): v
209
209
  * @param computation - A function that, when executed, returns the next trampoline step.
210
210
  * @returns A TrampolineThunk object containing the deferred computation.
211
211
  */
212
- export const makeTrampolineThunk = <T>(
213
- computation: () => Trampoline<T>
214
- ): TrampolineThunk<T> => ({
212
+ export const makeTrampolineThunk = <T>(computation: () => Trampoline<T>): TrampolineThunk<T> => ({
215
213
  isThunk: true, // Marker indicating this is a thunk
216
214
  fn: computation // The deferred computation function
217
215
  });
@@ -226,13 +224,11 @@ export const makeTrampolineThunk = <T>(
226
224
  * @param value - The value to test.
227
225
  * @returns True if the value is a valid TrampolineThunk, false otherwise.
228
226
  */
229
- export const isTrampolineThunk = <T>(
230
- value: Trampoline<T>
231
- ): value is TrampolineThunk<T> =>
227
+ export const isTrampolineThunk = <T>(value: Trampoline<T>): value is TrampolineThunk<T> =>
232
228
  typeof value === 'object' && // Must be an object
233
- value !== null && // Must not be null
234
- 'isThunk' in value && // Must have the 'isThunk' property
235
- value.isThunk; // The flag must be true
229
+ value !== null && // Must not be null
230
+ 'isThunk' in value && // Must have the 'isThunk' property
231
+ value.isThunk; // The flag must be true
236
232
 
237
233
  /**
238
234
  * Executes a trampoline computation until a final (non-thunk) result is obtained.
@@ -247,7 +243,8 @@ export const isTrampolineThunk = <T>(
247
243
  */
248
244
  export function trampoline<T>(initial: Trampoline<T>): T {
249
245
  let current = initial; // Start with the initial trampoline value
250
- while (isTrampolineThunk(current)) { // Keep unwrapping while we have thunks
246
+ while (isTrampolineThunk(current)) {
247
+ // Keep unwrapping while we have thunks
251
248
  current = current.fn(); // Execute the deferred function to get the next step
252
249
  }
253
250
  return current; // Once no thunks remain, return the final result
@@ -302,9 +299,7 @@ export function makeTrampoline<Args extends any[], Result>(
302
299
  * @param initial - The initial Trampoline or Promise of Trampoline to start execution from.
303
300
  * @returns A Promise that resolves to the final result (a non-thunk value).
304
301
  */
305
- export async function asyncTrampoline<T>(
306
- initial: Trampoline<T> | Promise<Trampoline<T>>
307
- ): Promise<T> {
302
+ export async function asyncTrampoline<T>(initial: Trampoline<T> | Promise<Trampoline<T>>): Promise<T> {
308
303
  let current = await initial; // Wait for the initial step to resolve if it's a Promise
309
304
 
310
305
  // Keep executing thunks until we reach a non-thunk (final) value
@@ -86,7 +86,7 @@
86
86
  tree.print(undefined, { isShowUndefined: true });
87
87
 
88
88
  const node3 = tree.getNode(3);
89
- if (node3) node3.right = tree.createNode(1);
89
+ if (node3) node3.right = tree._createNode(1);
90
90
  console.log(tree.isPerfectlyBalanced(), `tree.isPerfectlyBalanced()`);
91
91
  tree.print();
92
92