tree-set-typed 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. package/.eslintrc.js +61 -0
  2. package/.prettierignore +6 -0
  3. package/.prettierrc.js +16 -0
  4. package/LICENSE +21 -0
  5. package/README.md +482 -0
  6. package/coverage/clover.xml +13 -0
  7. package/coverage/coverage-final.json +96 -0
  8. package/coverage/coverage-summary.json +60 -0
  9. package/coverage/lcov-report/base.css +403 -0
  10. package/coverage/lcov-report/block-navigation.js +87 -0
  11. package/coverage/lcov-report/favicon.png +0 -0
  12. package/coverage/lcov-report/index.html +119 -0
  13. package/coverage/lcov-report/index.ts.html +109 -0
  14. package/coverage/lcov-report/prettify.css +1 -0
  15. package/coverage/lcov-report/prettify.js +2 -0
  16. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  17. package/coverage/lcov-report/sorter.js +206 -0
  18. package/coverage/lcov.info +14 -0
  19. package/dist/cjs/index.cjs +12 -0
  20. package/dist/cjs/index.cjs.map +1 -0
  21. package/dist/cjs-legacy/index.cjs +12 -0
  22. package/dist/cjs-legacy/index.cjs.map +1 -0
  23. package/dist/esm/index.mjs +3 -0
  24. package/dist/esm/index.mjs.map +1 -0
  25. package/dist/esm-legacy/index.mjs +3 -0
  26. package/dist/esm-legacy/index.mjs.map +1 -0
  27. package/dist/types/common/index.d.ts +12 -0
  28. package/dist/types/constants/index.d.ts +4 -0
  29. package/dist/types/data-structures/base/index.d.ts +2 -0
  30. package/dist/types/data-structures/base/iterable-element-base.d.ts +219 -0
  31. package/dist/types/data-structures/base/iterable-entry-base.d.ts +150 -0
  32. package/dist/types/data-structures/base/linear-base.d.ts +335 -0
  33. package/dist/types/data-structures/binary-tree/avl-tree-counter.d.ts +236 -0
  34. package/dist/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +197 -0
  35. package/dist/types/data-structures/binary-tree/avl-tree.d.ts +440 -0
  36. package/dist/types/data-structures/binary-tree/binary-indexed-tree.d.ts +174 -0
  37. package/dist/types/data-structures/binary-tree/binary-tree.d.ts +807 -0
  38. package/dist/types/data-structures/binary-tree/bst.d.ts +645 -0
  39. package/dist/types/data-structures/binary-tree/index.d.ts +10 -0
  40. package/dist/types/data-structures/binary-tree/red-black-tree.d.ts +312 -0
  41. package/dist/types/data-structures/binary-tree/segment-tree.d.ts +160 -0
  42. package/dist/types/data-structures/binary-tree/tree-counter.d.ts +243 -0
  43. package/dist/types/data-structures/binary-tree/tree-multi-map.d.ts +333 -0
  44. package/dist/types/data-structures/graph/abstract-graph.d.ts +340 -0
  45. package/dist/types/data-structures/graph/directed-graph.d.ts +332 -0
  46. package/dist/types/data-structures/graph/index.d.ts +4 -0
  47. package/dist/types/data-structures/graph/map-graph.d.ts +78 -0
  48. package/dist/types/data-structures/graph/undirected-graph.d.ts +347 -0
  49. package/dist/types/data-structures/hash/hash-map.d.ts +428 -0
  50. package/dist/types/data-structures/hash/index.d.ts +1 -0
  51. package/dist/types/data-structures/heap/heap.d.ts +552 -0
  52. package/dist/types/data-structures/heap/index.d.ts +3 -0
  53. package/dist/types/data-structures/heap/max-heap.d.ts +32 -0
  54. package/dist/types/data-structures/heap/min-heap.d.ts +33 -0
  55. package/dist/types/data-structures/index.d.ts +12 -0
  56. package/dist/types/data-structures/linked-list/doubly-linked-list.d.ts +437 -0
  57. package/dist/types/data-structures/linked-list/index.d.ts +3 -0
  58. package/dist/types/data-structures/linked-list/singly-linked-list.d.ts +567 -0
  59. package/dist/types/data-structures/linked-list/skip-linked-list.d.ts +27 -0
  60. package/dist/types/data-structures/matrix/index.d.ts +2 -0
  61. package/dist/types/data-structures/matrix/matrix.d.ts +168 -0
  62. package/dist/types/data-structures/matrix/navigator.d.ts +55 -0
  63. package/dist/types/data-structures/priority-queue/index.d.ts +3 -0
  64. package/dist/types/data-structures/priority-queue/max-priority-queue.d.ts +27 -0
  65. package/dist/types/data-structures/priority-queue/min-priority-queue.d.ts +26 -0
  66. package/dist/types/data-structures/priority-queue/priority-queue.d.ts +15 -0
  67. package/dist/types/data-structures/queue/deque.d.ts +459 -0
  68. package/dist/types/data-structures/queue/index.d.ts +2 -0
  69. package/dist/types/data-structures/queue/queue.d.ts +364 -0
  70. package/dist/types/data-structures/stack/index.d.ts +1 -0
  71. package/dist/types/data-structures/stack/stack.d.ts +324 -0
  72. package/dist/types/data-structures/tree/index.d.ts +1 -0
  73. package/dist/types/data-structures/tree/tree.d.ts +62 -0
  74. package/dist/types/data-structures/trie/index.d.ts +1 -0
  75. package/dist/types/data-structures/trie/trie.d.ts +412 -0
  76. package/dist/types/index.d.ts +23 -0
  77. package/dist/types/interfaces/binary-tree.d.ts +60 -0
  78. package/dist/types/interfaces/doubly-linked-list.d.ts +1 -0
  79. package/dist/types/interfaces/graph.d.ts +21 -0
  80. package/dist/types/interfaces/heap.d.ts +1 -0
  81. package/dist/types/interfaces/index.d.ts +8 -0
  82. package/dist/types/interfaces/navigator.d.ts +1 -0
  83. package/dist/types/interfaces/priority-queue.d.ts +1 -0
  84. package/dist/types/interfaces/segment-tree.d.ts +1 -0
  85. package/dist/types/interfaces/singly-linked-list.d.ts +1 -0
  86. package/dist/types/types/common.d.ts +15 -0
  87. package/dist/types/types/data-structures/base/base.d.ts +13 -0
  88. package/dist/types/types/data-structures/base/index.d.ts +1 -0
  89. package/dist/types/types/data-structures/binary-tree/avl-tree-counter.d.ts +2 -0
  90. package/dist/types/types/data-structures/binary-tree/avl-tree-multi-map.d.ts +2 -0
  91. package/dist/types/types/data-structures/binary-tree/avl-tree.d.ts +2 -0
  92. package/dist/types/types/data-structures/binary-tree/binary-indexed-tree.d.ts +1 -0
  93. package/dist/types/types/data-structures/binary-tree/binary-tree.d.ts +29 -0
  94. package/dist/types/types/data-structures/binary-tree/bst.d.ts +12 -0
  95. package/dist/types/types/data-structures/binary-tree/index.d.ts +9 -0
  96. package/dist/types/types/data-structures/binary-tree/red-black-tree.d.ts +3 -0
  97. package/dist/types/types/data-structures/binary-tree/segment-tree.d.ts +1 -0
  98. package/dist/types/types/data-structures/binary-tree/tree-counter.d.ts +2 -0
  99. package/dist/types/types/data-structures/binary-tree/tree-multi-map.d.ts +2 -0
  100. package/dist/types/types/data-structures/graph/abstract-graph.d.ts +14 -0
  101. package/dist/types/types/data-structures/graph/directed-graph.d.ts +1 -0
  102. package/dist/types/types/data-structures/graph/index.d.ts +3 -0
  103. package/dist/types/types/data-structures/graph/map-graph.d.ts +1 -0
  104. package/dist/types/types/data-structures/graph/undirected-graph.d.ts +1 -0
  105. package/dist/types/types/data-structures/hash/hash-map.d.ts +19 -0
  106. package/dist/types/types/data-structures/hash/index.d.ts +2 -0
  107. package/dist/types/types/data-structures/heap/heap.d.ts +5 -0
  108. package/dist/types/types/data-structures/heap/index.d.ts +1 -0
  109. package/dist/types/types/data-structures/heap/max-heap.d.ts +1 -0
  110. package/dist/types/types/data-structures/heap/min-heap.d.ts +1 -0
  111. package/dist/types/types/data-structures/index.d.ts +12 -0
  112. package/dist/types/types/data-structures/linked-list/doubly-linked-list.d.ts +2 -0
  113. package/dist/types/types/data-structures/linked-list/index.d.ts +3 -0
  114. package/dist/types/types/data-structures/linked-list/singly-linked-list.d.ts +2 -0
  115. package/dist/types/types/data-structures/linked-list/skip-linked-list.d.ts +4 -0
  116. package/dist/types/types/data-structures/matrix/index.d.ts +2 -0
  117. package/dist/types/types/data-structures/matrix/matrix.d.ts +7 -0
  118. package/dist/types/types/data-structures/matrix/navigator.d.ts +14 -0
  119. package/dist/types/types/data-structures/priority-queue/index.d.ts +3 -0
  120. package/dist/types/types/data-structures/priority-queue/max-priority-queue.d.ts +1 -0
  121. package/dist/types/types/data-structures/priority-queue/min-priority-queue.d.ts +1 -0
  122. package/dist/types/types/data-structures/priority-queue/priority-queue.d.ts +2 -0
  123. package/dist/types/types/data-structures/queue/deque.d.ts +4 -0
  124. package/dist/types/types/data-structures/queue/index.d.ts +2 -0
  125. package/dist/types/types/data-structures/queue/queue.d.ts +4 -0
  126. package/dist/types/types/data-structures/stack/index.d.ts +1 -0
  127. package/dist/types/types/data-structures/stack/stack.d.ts +2 -0
  128. package/dist/types/types/data-structures/tree/index.d.ts +1 -0
  129. package/dist/types/types/data-structures/tree/tree.d.ts +1 -0
  130. package/dist/types/types/data-structures/trie/index.d.ts +1 -0
  131. package/dist/types/types/data-structures/trie/trie.d.ts +4 -0
  132. package/dist/types/types/index.d.ts +3 -0
  133. package/dist/types/types/utils/index.d.ts +2 -0
  134. package/dist/types/types/utils/utils.d.ts +22 -0
  135. package/dist/types/types/utils/validate-type.d.ts +19 -0
  136. package/dist/types/utils/index.d.ts +2 -0
  137. package/dist/types/utils/number.d.ts +14 -0
  138. package/dist/types/utils/utils.d.ts +209 -0
  139. package/dist/umd/red-black-tree-typed.js +14578 -0
  140. package/dist/umd/red-black-tree-typed.js.map +1 -0
  141. package/dist/umd/red-black-tree-typed.min.js +44 -0
  142. package/dist/umd/red-black-tree-typed.min.js.map +1 -0
  143. package/docs/.nojekyll +1 -0
  144. package/docs/assets/highlight.css +92 -0
  145. package/docs/assets/main.js +59 -0
  146. package/docs/assets/navigation.js +1 -0
  147. package/docs/assets/search.js +1 -0
  148. package/docs/assets/style.css +1383 -0
  149. package/docs/classes/AVLTree.html +2046 -0
  150. package/docs/classes/AVLTreeNode.html +263 -0
  151. package/docs/index.html +523 -0
  152. package/docs/modules.html +45 -0
  153. package/jest.config.js +8 -0
  154. package/package.json +113 -0
  155. package/src/common/index.ts +23 -0
  156. package/src/constants/index.ts +4 -0
  157. package/src/data-structures/base/index.ts +2 -0
  158. package/src/data-structures/base/iterable-element-base.ts +352 -0
  159. package/src/data-structures/base/iterable-entry-base.ts +246 -0
  160. package/src/data-structures/base/linear-base.ts +643 -0
  161. package/src/data-structures/binary-tree/avl-tree-counter.ts +539 -0
  162. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +438 -0
  163. package/src/data-structures/binary-tree/avl-tree.ts +840 -0
  164. package/src/data-structures/binary-tree/binary-indexed-tree.ts +331 -0
  165. package/src/data-structures/binary-tree/binary-tree.ts +2492 -0
  166. package/src/data-structures/binary-tree/bst.ts +2024 -0
  167. package/src/data-structures/binary-tree/index.ts +10 -0
  168. package/src/data-structures/binary-tree/red-black-tree.ts +767 -0
  169. package/src/data-structures/binary-tree/segment-tree.ts +324 -0
  170. package/src/data-structures/binary-tree/tree-counter.ts +575 -0
  171. package/src/data-structures/binary-tree/tree-multi-map.ts +549 -0
  172. package/src/data-structures/graph/abstract-graph.ts +1081 -0
  173. package/src/data-structures/graph/directed-graph.ts +715 -0
  174. package/src/data-structures/graph/index.ts +4 -0
  175. package/src/data-structures/graph/map-graph.ts +132 -0
  176. package/src/data-structures/graph/undirected-graph.ts +626 -0
  177. package/src/data-structures/hash/hash-map.ts +813 -0
  178. package/src/data-structures/hash/index.ts +1 -0
  179. package/src/data-structures/heap/heap.ts +1020 -0
  180. package/src/data-structures/heap/index.ts +3 -0
  181. package/src/data-structures/heap/max-heap.ts +47 -0
  182. package/src/data-structures/heap/min-heap.ts +36 -0
  183. package/src/data-structures/index.ts +12 -0
  184. package/src/data-structures/linked-list/doubly-linked-list.ts +876 -0
  185. package/src/data-structures/linked-list/index.ts +3 -0
  186. package/src/data-structures/linked-list/singly-linked-list.ts +1050 -0
  187. package/src/data-structures/linked-list/skip-linked-list.ts +173 -0
  188. package/src/data-structures/matrix/index.ts +2 -0
  189. package/src/data-structures/matrix/matrix.ts +491 -0
  190. package/src/data-structures/matrix/navigator.ts +124 -0
  191. package/src/data-structures/priority-queue/index.ts +3 -0
  192. package/src/data-structures/priority-queue/max-priority-queue.ts +42 -0
  193. package/src/data-structures/priority-queue/min-priority-queue.ts +29 -0
  194. package/src/data-structures/priority-queue/priority-queue.ts +19 -0
  195. package/src/data-structures/queue/deque.ts +1001 -0
  196. package/src/data-structures/queue/index.ts +2 -0
  197. package/src/data-structures/queue/queue.ts +592 -0
  198. package/src/data-structures/stack/index.ts +1 -0
  199. package/src/data-structures/stack/stack.ts +469 -0
  200. package/src/data-structures/tree/index.ts +1 -0
  201. package/src/data-structures/tree/tree.ts +115 -0
  202. package/src/data-structures/trie/index.ts +1 -0
  203. package/src/data-structures/trie/trie.ts +756 -0
  204. package/src/index.ts +24 -0
  205. package/src/interfaces/binary-tree.ts +252 -0
  206. package/src/interfaces/doubly-linked-list.ts +1 -0
  207. package/src/interfaces/graph.ts +44 -0
  208. package/src/interfaces/heap.ts +1 -0
  209. package/src/interfaces/index.ts +8 -0
  210. package/src/interfaces/navigator.ts +1 -0
  211. package/src/interfaces/priority-queue.ts +1 -0
  212. package/src/interfaces/segment-tree.ts +1 -0
  213. package/src/interfaces/singly-linked-list.ts +1 -0
  214. package/src/types/common.ts +25 -0
  215. package/src/types/data-structures/base/base.ts +34 -0
  216. package/src/types/data-structures/base/index.ts +1 -0
  217. package/src/types/data-structures/binary-tree/avl-tree-counter.ts +3 -0
  218. package/src/types/data-structures/binary-tree/avl-tree-multi-map.ts +3 -0
  219. package/src/types/data-structures/binary-tree/avl-tree.ts +3 -0
  220. package/src/types/data-structures/binary-tree/binary-indexed-tree.ts +1 -0
  221. package/src/types/data-structures/binary-tree/binary-tree.ts +31 -0
  222. package/src/types/data-structures/binary-tree/bst.ts +19 -0
  223. package/src/types/data-structures/binary-tree/index.ts +9 -0
  224. package/src/types/data-structures/binary-tree/red-black-tree.ts +5 -0
  225. package/src/types/data-structures/binary-tree/segment-tree.ts +1 -0
  226. package/src/types/data-structures/binary-tree/tree-counter.ts +3 -0
  227. package/src/types/data-structures/binary-tree/tree-multi-map.ts +3 -0
  228. package/src/types/data-structures/graph/abstract-graph.ts +18 -0
  229. package/src/types/data-structures/graph/directed-graph.ts +2 -0
  230. package/src/types/data-structures/graph/index.ts +3 -0
  231. package/src/types/data-structures/graph/map-graph.ts +1 -0
  232. package/src/types/data-structures/graph/undirected-graph.ts +1 -0
  233. package/src/types/data-structures/hash/hash-map.ts +19 -0
  234. package/src/types/data-structures/hash/index.ts +3 -0
  235. package/src/types/data-structures/heap/heap.ts +6 -0
  236. package/src/types/data-structures/heap/index.ts +1 -0
  237. package/src/types/data-structures/heap/max-heap.ts +1 -0
  238. package/src/types/data-structures/heap/min-heap.ts +1 -0
  239. package/src/types/data-structures/index.ts +12 -0
  240. package/src/types/data-structures/linked-list/doubly-linked-list.ts +3 -0
  241. package/src/types/data-structures/linked-list/index.ts +3 -0
  242. package/src/types/data-structures/linked-list/singly-linked-list.ts +3 -0
  243. package/src/types/data-structures/linked-list/skip-linked-list.ts +1 -0
  244. package/src/types/data-structures/matrix/index.ts +2 -0
  245. package/src/types/data-structures/matrix/matrix.ts +7 -0
  246. package/src/types/data-structures/matrix/navigator.ts +14 -0
  247. package/src/types/data-structures/priority-queue/index.ts +3 -0
  248. package/src/types/data-structures/priority-queue/max-priority-queue.ts +1 -0
  249. package/src/types/data-structures/priority-queue/min-priority-queue.ts +1 -0
  250. package/src/types/data-structures/priority-queue/priority-queue.ts +3 -0
  251. package/src/types/data-structures/queue/deque.ts +5 -0
  252. package/src/types/data-structures/queue/index.ts +2 -0
  253. package/src/types/data-structures/queue/queue.ts +5 -0
  254. package/src/types/data-structures/stack/index.ts +1 -0
  255. package/src/types/data-structures/stack/stack.ts +3 -0
  256. package/src/types/data-structures/tree/index.ts +1 -0
  257. package/src/types/data-structures/tree/tree.ts +1 -0
  258. package/src/types/data-structures/trie/index.ts +1 -0
  259. package/src/types/data-structures/trie/trie.ts +3 -0
  260. package/src/types/index.ts +3 -0
  261. package/src/types/utils/index.ts +2 -0
  262. package/src/types/utils/utils.ts +33 -0
  263. package/src/types/utils/validate-type.ts +35 -0
  264. package/src/utils/index.ts +2 -0
  265. package/src/utils/number.ts +22 -0
  266. package/src/utils/utils.ts +350 -0
  267. package/test/index.test.ts +111 -0
  268. package/tsconfig.base.json +23 -0
  269. package/tsconfig.json +12 -0
  270. package/tsconfig.test.json +8 -0
  271. package/tsconfig.types.json +15 -0
  272. package/tsup.config.js +28 -0
  273. package/tsup.node.config.js +71 -0
@@ -0,0 +1,756 @@
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
+
9
+ import type { ElementCallback, TrieOptions } from '../../types';
10
+ import { IterableElementBase } from '../base';
11
+
12
+ /**
13
+ * Node used by Trie to store one character and its children.
14
+ * @remarks Time O(1), Space O(1)
15
+ */
16
+ export class TrieNode {
17
+ /**
18
+ * Create a Trie node with a character key.
19
+ * @remarks Time O(1), Space O(1)
20
+ * @returns New TrieNode instance.
21
+ */
22
+
23
+ constructor(key: string) {
24
+ this._key = key;
25
+ this._isEnd = false;
26
+ this._children = new Map<string, TrieNode>();
27
+ }
28
+
29
+ protected _key: string;
30
+
31
+ /**
32
+ * Get the character key of this node.
33
+ * @remarks Time O(1), Space O(1)
34
+ * @returns Character key string.
35
+ */
36
+
37
+ get key(): string {
38
+ return this._key;
39
+ }
40
+
41
+ /**
42
+ * Set the character key of this node.
43
+ * @remarks Time O(1), Space O(1)
44
+ * @param value - New character key.
45
+ * @returns void
46
+ */
47
+
48
+ set key(value: string) {
49
+ this._key = value;
50
+ }
51
+
52
+ protected _children: Map<string, TrieNode>;
53
+
54
+ /**
55
+ * Get the child map of this node.
56
+ * @remarks Time O(1), Space O(1)
57
+ * @returns Map from character to child node.
58
+ */
59
+
60
+ get children(): Map<string, TrieNode> {
61
+ return this._children;
62
+ }
63
+
64
+ /**
65
+ * Replace the child map of this node.
66
+ * @remarks Time O(1), Space O(1)
67
+ * @param value - New map of character → node.
68
+ * @returns void
69
+ */
70
+
71
+ set children(value: Map<string, TrieNode>) {
72
+ this._children = value;
73
+ }
74
+
75
+ protected _isEnd: boolean;
76
+
77
+ /**
78
+ * Check whether this node marks the end of a word.
79
+ * @remarks Time O(1), Space O(1)
80
+ * @returns True if this node ends a word.
81
+ */
82
+
83
+ get isEnd(): boolean {
84
+ return this._isEnd;
85
+ }
86
+
87
+ /**
88
+ * Mark this node as the end of a word or not.
89
+ * @remarks Time O(1), Space O(1)
90
+ * @param value - Whether this node ends a word.
91
+ * @returns void
92
+ */
93
+
94
+ set isEnd(value: boolean) {
95
+ this._isEnd = value;
96
+ }
97
+ }
98
+
99
+ /**
100
+ * Prefix tree (Trie) for fast prefix queries and word storage.
101
+ * @remarks Time O(1), Space O(1)
102
+ * @template R
103
+ * 1. Node Structure: Each node in a Trie represents a string (or a part of a string). The root node typically represents an empty string.
104
+ * 2. Child Node Relationship: Each node's children represent the strings that can be formed by adding one character to the string at the current node. For example, if a node represents the string 'ca', one of its children might represent 'cat'.
105
+ * 3. Fast Retrieval: Trie allows retrieval in O(m) time complexity, where m is the length of the string to be searched.
106
+ * 4. Space Efficiency: Trie can store a large number of strings very space-efficiently, especially when these strings share common prefixes.
107
+ * 5. Autocomplete and Prediction: Trie can be used for implementing autocomplete and word prediction features, as it can quickly find all strings with a common prefix.
108
+ * 6. Sorting: Trie can be used to sort a set of strings in alphabetical order.
109
+ * 7. String Retrieval: For example, searching for a specific string in a large set of strings.
110
+ * 8. Autocomplete: Providing recommended words or phrases as a user types.
111
+ * 9. Spell Check: Checking the spelling of words.
112
+ * 10. IP Routing: Used in certain types of IP routing algorithms.
113
+ * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
114
+ * @example
115
+ * // basic Trie creation and add words
116
+ * // Create a simple Trie with initial words
117
+ * const trie = new Trie(['apple', 'app', 'apply']);
118
+ *
119
+ * // Verify size
120
+ * console.log(trie.size); // 3;
121
+ *
122
+ * // Check if words exist
123
+ * console.log(trie.has('apple')); // true;
124
+ * console.log(trie.has('app')); // true;
125
+ *
126
+ * // Add a new word
127
+ * trie.add('application');
128
+ * console.log(trie.size); // 4;
129
+ * @example
130
+ * // Trie getWords and prefix search
131
+ * const trie = new Trie(['apple', 'app', 'apply', 'application', 'apricot']);
132
+ *
133
+ * // Get all words with prefix 'app'
134
+ * const appWords = trie.getWords('app');
135
+ * console.log(appWords); // contains 'app';
136
+ * console.log(appWords); // contains 'apple';
137
+ * console.log(appWords); // contains 'apply';
138
+ * console.log(appWords); // contains 'application';
139
+ * expect(appWords).not.toContain('apricot');
140
+ * @example
141
+ * // Trie isPrefix and isAbsolutePrefix checks
142
+ * const trie = new Trie(['tree', 'trial', 'trick', 'trip', 'trie']);
143
+ *
144
+ * // Check if string is a prefix of any word
145
+ * console.log(trie.hasPrefix('tri')); // true;
146
+ * console.log(trie.hasPrefix('tr')); // true;
147
+ * console.log(trie.hasPrefix('xyz')); // false;
148
+ *
149
+ * // Check if string is an absolute prefix (not a complete word)
150
+ * console.log(trie.hasPurePrefix('tri')); // true;
151
+ * console.log(trie.hasPurePrefix('tree')); // false; // 'tree' is a complete word
152
+ *
153
+ * // Verify size
154
+ * console.log(trie.size); // 5;
155
+ * @example
156
+ * // Trie delete and iteration
157
+ * const trie = new Trie(['car', 'card', 'care', 'careful', 'can', 'cat']);
158
+ *
159
+ * // Delete a word
160
+ * trie.delete('card');
161
+ * console.log(trie.has('card')); // false;
162
+ *
163
+ * // Word with same prefix still exists
164
+ * console.log(trie.has('care')); // true;
165
+ *
166
+ * // Size decreased
167
+ * console.log(trie.size); // 5;
168
+ *
169
+ * // Iterate through all words
170
+ * const allWords = [...trie];
171
+ * console.log(allWords.length); // 5;
172
+ * @example
173
+ * // Trie for autocomplete search index
174
+ * // Trie is perfect for autocomplete: O(m + k) where m is prefix length, k is results
175
+ * const searchIndex = new Trie(['typescript', 'javascript', 'python', 'java', 'rust', 'ruby', 'golang', 'kotlin']);
176
+ *
177
+ * // User types 'j' - get all suggestions
178
+ * const jResults = searchIndex.getWords('j');
179
+ * console.log(jResults); // contains 'javascript';
180
+ * console.log(jResults); // contains 'java';
181
+ * console.log(jResults.length); // 2;
182
+ *
183
+ * // User types 'ja' - get more specific suggestions
184
+ * const jaResults = searchIndex.getWords('ja');
185
+ * console.log(jaResults); // contains 'javascript';
186
+ * console.log(jaResults); // contains 'java';
187
+ * console.log(jaResults.length); // 2;
188
+ *
189
+ * // User types 'jav' - even more specific
190
+ * const javResults = searchIndex.getWords('jav');
191
+ * console.log(javResults); // contains 'javascript';
192
+ * console.log(javResults); // contains 'java';
193
+ * console.log(javResults.length); // 2;
194
+ *
195
+ * // Check for common prefix
196
+ *
197
+ * console.log(searchIndex.hasCommonPrefix('ja')); // false; // Not all words start with 'ja'
198
+ *
199
+ * // Total words in index
200
+ * console.log(searchIndex.size); // 8;
201
+ *
202
+ * // Get height (depth of tree)
203
+ * const height = searchIndex.getHeight();
204
+ * console.log(typeof height); // 'number';
205
+ * @example
206
+ * // Dictionary: Case-insensitive word lookup
207
+ * // Create a case-insensitive dictionary
208
+ * const dictionary = new Trie<string>([], { caseSensitive: false });
209
+ *
210
+ * // Add words with mixed casing
211
+ * dictionary.add('Hello');
212
+ * dictionary.add('WORLD');
213
+ * dictionary.add('JavaScript');
214
+ *
215
+ * // Test lookups with different casings
216
+ * console.log(dictionary.has('hello')); // true;
217
+ * console.log(dictionary.has('HELLO')); // true;
218
+ * console.log(dictionary.has('Hello')); // true;
219
+ * console.log(dictionary.has('javascript')); // true;
220
+ * console.log(dictionary.has('JAVASCRIPT')); // true;
221
+ * @example
222
+ * // File System Path Operations
223
+ * const fileSystem = new Trie<string>([
224
+ * '/home/user/documents/file1.txt',
225
+ * '/home/user/documents/file2.txt',
226
+ * '/home/user/pictures/photo.jpg',
227
+ * '/home/user/pictures/vacation/',
228
+ * '/home/user/downloads'
229
+ * ]);
230
+ *
231
+ * // Find common directory prefix
232
+ * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/';
233
+ *
234
+ * // List all files in a directory
235
+ * const documentsFiles = fileSystem.getWords('/home/user/documents/');
236
+ * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt'];
237
+ * @example
238
+ * // IP Address Routing Table
239
+ * // Add IP address prefixes and their corresponding routes
240
+ * const routes = {
241
+ * '192.168.1': 'LAN_SUBNET_1',
242
+ * '192.168.2': 'LAN_SUBNET_2',
243
+ * '10.0.0': 'PRIVATE_NETWORK_1',
244
+ * '10.0.1': 'PRIVATE_NETWORK_2'
245
+ * };
246
+ *
247
+ * const ipRoutingTable = new Trie<string>(Object.keys(routes));
248
+ *
249
+ * // Check IP address prefix matching
250
+ * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true;
251
+ * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true;
252
+ *
253
+ * // Validate IP address belongs to subnet
254
+ * const ip = '192.168.1.100';
255
+ * const subnet = ip.split('.').slice(0, 3).join('.');
256
+ * console.log(ipRoutingTable.hasPrefix(subnet)); // true;
257
+ */
258
+ export class Trie<R = any> extends IterableElementBase<string, R> {
259
+ /**
260
+ * Create a Trie and optionally bulk-insert words.
261
+ * @remarks Time O(totalChars), Space O(totalChars)
262
+ * @param [words] - Iterable of strings (or raw records if toElementFn is provided).
263
+ * @param [options] - Options such as toElementFn and caseSensitive.
264
+ * @returns New Trie instance.
265
+ */
266
+
267
+ constructor(words: Iterable<string> | Iterable<R> = [], options?: TrieOptions<R>) {
268
+ super(options);
269
+ if (options) {
270
+ const { caseSensitive } = options;
271
+ if (caseSensitive !== undefined) this._caseSensitive = caseSensitive;
272
+ }
273
+ if (words) {
274
+ this.addMany(words);
275
+ }
276
+ }
277
+
278
+ protected _size: number = 0;
279
+
280
+ /**
281
+ * Get the number of stored words.
282
+ * @remarks Time O(1), Space O(1)
283
+ * @returns Word count.
284
+ */
285
+
286
+ get size(): number {
287
+ return this._size;
288
+ }
289
+
290
+ protected _caseSensitive: boolean = true;
291
+
292
+ /**
293
+ * Get whether comparisons are case-sensitive.
294
+ * @remarks Time O(1), Space O(1)
295
+ * @returns True if case-sensitive.
296
+ */
297
+
298
+ get caseSensitive(): boolean {
299
+ return this._caseSensitive;
300
+ }
301
+
302
+ protected _root: TrieNode = new TrieNode('');
303
+
304
+ /**
305
+ * Get the root node.
306
+ * @remarks Time O(1), Space O(1)
307
+ * @returns Root TrieNode.
308
+ */
309
+
310
+ get root() {
311
+ return this._root;
312
+ }
313
+
314
+ /**
315
+ * (Protected) Get total count for base class iteration.
316
+ * @remarks Time O(1), Space O(1)
317
+ * @returns Total number of elements.
318
+ */
319
+
320
+ protected get _total() {
321
+ return this._size;
322
+ }
323
+
324
+ /**
325
+ * Insert one word into the trie.
326
+ * @remarks Time O(L), Space O(L)
327
+ * @param word - Word to insert.
328
+ * @returns True if the word was newly added.
329
+ */
330
+
331
+ add(word: string): boolean {
332
+ word = this._caseProcess(word);
333
+ let cur = this.root;
334
+ let isNewWord = false;
335
+ for (const c of word) {
336
+ let nodeC = cur.children.get(c);
337
+ if (!nodeC) {
338
+ nodeC = new TrieNode(c);
339
+ cur.children.set(c, nodeC);
340
+ }
341
+ cur = nodeC;
342
+ }
343
+ if (!cur.isEnd) {
344
+ isNewWord = true;
345
+ cur.isEnd = true;
346
+ this._size++;
347
+ }
348
+ return isNewWord;
349
+ }
350
+
351
+ /**
352
+ * Insert many words from an iterable.
353
+ * @remarks Time O(ΣL), Space O(ΣL)
354
+ * @param words - Iterable of strings (or raw records if toElementFn is provided).
355
+ * @returns Array of per-word 'added' flags.
356
+ */
357
+
358
+ addMany(words: Iterable<string> | Iterable<R>): boolean[] {
359
+ const ans: boolean[] = [];
360
+ for (const word of words) {
361
+ if (this.toElementFn) {
362
+ ans.push(this.add(this.toElementFn(word as R)));
363
+ } else {
364
+ ans.push(this.add(word as string));
365
+ }
366
+ }
367
+ return ans;
368
+ }
369
+
370
+ /**
371
+ * Check whether a word exists.
372
+ * @remarks Time O(L), Space O(1)
373
+ * @param word - Word to search for.
374
+ * @returns True if present.
375
+ */
376
+
377
+ override has(word: string): boolean {
378
+ word = this._caseProcess(word);
379
+ let cur = this.root;
380
+ for (const c of word) {
381
+ const nodeC = cur.children.get(c);
382
+ if (!nodeC) return false;
383
+ cur = nodeC;
384
+ }
385
+ return cur.isEnd;
386
+ }
387
+
388
+ /**
389
+ * Check whether the trie is empty.
390
+ * @remarks Time O(1), Space O(1)
391
+ * @returns True if size is 0.
392
+ */
393
+
394
+ isEmpty(): boolean {
395
+ return this._size === 0;
396
+ }
397
+
398
+ /**
399
+ * Remove all words and reset to a fresh root.
400
+ * @remarks Time O(1), Space O(1)
401
+ * @returns void
402
+ */
403
+
404
+ clear(): void {
405
+ this._size = 0;
406
+ this._root = new TrieNode('');
407
+ }
408
+
409
+ /**
410
+ * Delete one word if present.
411
+ * @remarks Time O(L), Space O(1)
412
+ * @param word - Word to delete.
413
+ * @returns True if a word was removed.
414
+ */
415
+
416
+ delete(word: string): boolean {
417
+ word = this._caseProcess(word);
418
+ let isDeleted = false;
419
+ const dfs = (cur: TrieNode, i: number): boolean => {
420
+ const char = word[i];
421
+ const child = cur.children.get(char);
422
+ if (child) {
423
+ if (i === word.length - 1) {
424
+ if (child.isEnd) {
425
+ if (child.children.size > 0) {
426
+ child.isEnd = false;
427
+ } else {
428
+ cur.children.delete(char);
429
+ }
430
+ isDeleted = true;
431
+ return true;
432
+ }
433
+ return false;
434
+ }
435
+ const res = dfs(child, i + 1);
436
+ if (res && !cur.isEnd && child.children.size === 0) {
437
+ cur.children.delete(char);
438
+ return true;
439
+ }
440
+ return false;
441
+ }
442
+ return false;
443
+ };
444
+
445
+ dfs(this.root, 0);
446
+ if (isDeleted) {
447
+ this._size--;
448
+ }
449
+ return isDeleted;
450
+ }
451
+
452
+ /**
453
+ * Compute the height (max depth) of the trie.
454
+ * @remarks Time O(N), Space O(H)
455
+ * @returns Maximum depth from root to a leaf.
456
+ */
457
+
458
+ getHeight(): number {
459
+ const startNode = this.root;
460
+ let maxDepth = 0;
461
+ if (startNode) {
462
+ const bfs = (node: TrieNode, level: number) => {
463
+ if (level > maxDepth) {
464
+ maxDepth = level;
465
+ }
466
+ const { children } = node;
467
+ if (children) {
468
+ for (const child of children.entries()) {
469
+ bfs(child[1], level + 1);
470
+ }
471
+ }
472
+ };
473
+ bfs(startNode, 0);
474
+ }
475
+ return maxDepth;
476
+ }
477
+
478
+ /**
479
+ * Check whether input is a proper prefix of at least one word.
480
+ * @remarks Time O(L), Space O(1)
481
+ * @param input - String to test as prefix.
482
+ * @returns True if input is a prefix but not a full word.
483
+ */
484
+
485
+ hasPurePrefix(input: string): boolean {
486
+ input = this._caseProcess(input);
487
+ let cur = this.root;
488
+ for (const c of input) {
489
+ const nodeC = cur.children.get(c);
490
+ if (!nodeC) return false;
491
+ cur = nodeC;
492
+ }
493
+ return !cur.isEnd;
494
+ }
495
+
496
+ /**
497
+ * Check whether any word starts with input.
498
+ * @remarks Time O(L), Space O(1)
499
+ * @param input - String to test as prefix.
500
+ * @returns True if input matches a path from root.
501
+ */
502
+
503
+ hasPrefix(input: string): boolean {
504
+ input = this._caseProcess(input);
505
+ let cur = this.root;
506
+ for (const c of input) {
507
+ const nodeC = cur.children.get(c);
508
+ if (!nodeC) return false;
509
+ cur = nodeC;
510
+ }
511
+ return true;
512
+ }
513
+
514
+ /**
515
+ * Check whether the trie’s longest common prefix equals input.
516
+ * @remarks Time O(min(H,L)), Space O(1)
517
+ * @param input - Candidate longest common prefix.
518
+ * @returns True if input equals the common prefix.
519
+ */
520
+
521
+ hasCommonPrefix(input: string): boolean {
522
+ input = this._caseProcess(input);
523
+ let commonPre = '';
524
+ const dfs = (cur: TrieNode) => {
525
+ commonPre += cur.key;
526
+ if (commonPre === input) return;
527
+ if (cur.isEnd) return;
528
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
529
+ else return;
530
+ };
531
+ dfs(this.root);
532
+ return commonPre === input;
533
+ }
534
+
535
+ /**
536
+ * Return the longest common prefix among all words.
537
+ * @remarks Time O(H), Space O(1)
538
+ * @returns The longest common prefix string.
539
+ */
540
+
541
+ getLongestCommonPrefix(): string {
542
+ let commonPre = '';
543
+ const dfs = (cur: TrieNode) => {
544
+ commonPre += cur.key;
545
+ if (cur.isEnd) return;
546
+ if (cur && cur.children && cur.children.size === 1) dfs(Array.from(cur.children.values())[0]);
547
+ else return;
548
+ };
549
+ dfs(this.root);
550
+ return commonPre;
551
+ }
552
+
553
+ /**
554
+ * Collect words under a prefix up to a maximum count.
555
+ * @remarks Time O(K·L), Space O(K·L)
556
+ * @param [prefix] - Prefix to match; default empty string for root.
557
+ * @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
558
+ * @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
559
+ * @returns Array of collected words (at most max).
560
+ */
561
+
562
+ getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false): string[] {
563
+ prefix = this._caseProcess(prefix);
564
+ const words: string[] = [];
565
+ let found = 0;
566
+
567
+ function dfs(node: TrieNode, word: string) {
568
+ for (const char of node.children.keys()) {
569
+ const charNode = node.children.get(char);
570
+ if (charNode !== undefined) {
571
+ dfs(charNode, word.concat(char));
572
+ }
573
+ }
574
+ if (node.isEnd) {
575
+ if (found > max - 1) return;
576
+ words.push(word);
577
+ found++;
578
+ }
579
+ }
580
+
581
+ let startNode = this.root;
582
+
583
+ if (prefix) {
584
+ for (const c of prefix) {
585
+ const nodeC = startNode.children.get(c);
586
+ if (nodeC) {
587
+ startNode = nodeC;
588
+ } else {
589
+ return [];
590
+ }
591
+ }
592
+ }
593
+
594
+ if (isAllWhenEmptyPrefix || startNode !== this.root) dfs(startNode, prefix);
595
+
596
+ return words;
597
+ }
598
+
599
+ /**
600
+ * Deep clone this trie by iterating and inserting all words.
601
+ * @remarks Time O(ΣL), Space O(ΣL)
602
+ * @returns A new trie with the same words and options.
603
+ */
604
+
605
+ clone(): this {
606
+ const next = this._createInstance();
607
+ for (const x of this) next.add(x);
608
+ return next;
609
+ }
610
+
611
+ /**
612
+ * Filter words into a new trie of the same class.
613
+ * @remarks Time O(ΣL), Space O(ΣL)
614
+ * @param predicate - Predicate (word, index, trie) → boolean to keep word.
615
+ * @param [thisArg] - Value for `this` inside the predicate.
616
+ * @returns A new trie containing words that satisfy the predicate.
617
+ */
618
+
619
+ filter(predicate: ElementCallback<string, R, boolean>, thisArg?: any): this {
620
+ const results = this._createInstance();
621
+ let index = 0;
622
+ for (const word of this) {
623
+ if (predicate.call(thisArg, word, index, this)) {
624
+ results.add(word);
625
+ }
626
+ index++;
627
+ }
628
+ return results;
629
+ }
630
+
631
+ map<RM>(callback: ElementCallback<string, R, string>, options?: TrieOptions<RM>, thisArg?: any): Trie<RM>;
632
+
633
+ /**
634
+ * Map words into a new trie (possibly different record type).
635
+ * @remarks Time O(ΣL), Space O(ΣL)
636
+ * @template EM
637
+ * @template RM
638
+ * @param callback - Mapping function (word, index, trie) → newWord (string).
639
+ * @param [options] - Options for the output trie (e.g., toElementFn, caseSensitive).
640
+ * @param [thisArg] - Value for `this` inside the callback.
641
+ * @returns A new Trie constructed from mapped words.
642
+ */
643
+
644
+ map<EM, RM>(
645
+ callback: ElementCallback<string, R, EM>,
646
+ options?: TrieOptions<RM>,
647
+ thisArg?: any
648
+ ): IterableElementBase<EM, RM>;
649
+
650
+ map<EM, RM>(callback: ElementCallback<string, R, EM>, options?: TrieOptions<RM>, thisArg?: any): any {
651
+ const newTrie = this._createLike<RM>([], options);
652
+ let i = 0;
653
+ for (const x of this) {
654
+ const v = thisArg === undefined ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
655
+ if (typeof v !== 'string') {
656
+ throw new TypeError(`Trie.map callback must return string; got ${typeof v}`);
657
+ }
658
+ newTrie.add(v);
659
+ }
660
+ return newTrie;
661
+ }
662
+
663
+ /**
664
+ * Map words into a new trie of the same element type.
665
+ * @remarks Time O(ΣL), Space O(ΣL)
666
+ * @param callback - Mapping function (word, index, trie) → string.
667
+ * @param [thisArg] - Value for `this` inside the callback.
668
+ * @returns A new trie with mapped words.
669
+ */
670
+
671
+ mapSame(callback: ElementCallback<string, R, string>, thisArg?: any): this {
672
+ const next = this._createInstance();
673
+ let i = 0;
674
+ for (const key of this) {
675
+ const mapped = thisArg === undefined ? callback(key, i++, this) : callback.call(thisArg, key, i++, this);
676
+ next.add(mapped);
677
+ }
678
+ return next;
679
+ }
680
+
681
+ /**
682
+ * (Protected) Create an empty instance of the same concrete class.
683
+ * @remarks Time O(1), Space O(1)
684
+ * @param [options] - Options forwarded to the constructor.
685
+ * @returns An empty like-kind trie instance.
686
+ */
687
+
688
+ protected _createInstance(options?: TrieOptions<R>): this {
689
+ const Ctor: any = this.constructor;
690
+ const next: any = new Ctor([], {
691
+ toElementFn: this.toElementFn,
692
+ caseSensitive: this.caseSensitive,
693
+ ...(options ?? {})
694
+ });
695
+ return next as this;
696
+ }
697
+
698
+ /**
699
+ * (Protected) Create a like-kind trie and seed it from an iterable.
700
+ * @remarks Time O(ΣL), Space O(ΣL)
701
+ * @template RM
702
+ * @param [elements] - Iterable used to seed the new trie.
703
+ * @param [options] - Options forwarded to the constructor.
704
+ * @returns A like-kind Trie instance.
705
+ */
706
+
707
+ protected _createLike<RM>(elements: Iterable<string> | Iterable<RM> = [], options?: TrieOptions<RM>): Trie<RM> {
708
+ const Ctor: any = this.constructor;
709
+ return new Ctor(elements, options) as Trie<RM>;
710
+ }
711
+
712
+ /**
713
+ * (Protected) Spawn an empty like-kind trie instance.
714
+ * @remarks Time O(1), Space O(1)
715
+ * @template RM
716
+ * @param [options] - Options forwarded to the constructor.
717
+ * @returns An empty like-kind Trie instance.
718
+ */
719
+
720
+ protected _spawnLike<RM>(options?: TrieOptions<RM>): Trie<RM> {
721
+ return this._createLike<RM>([], options);
722
+ }
723
+
724
+ /**
725
+ * (Protected) Iterate all words in lexicographic order of edges.
726
+ * @remarks Time O(ΣL), Space O(H)
727
+ * @returns Iterator of words.
728
+ */
729
+
730
+ protected *_getIterator(): IterableIterator<string> {
731
+ function* _dfs(node: TrieNode, path: string): IterableIterator<string> {
732
+ if (node.isEnd) {
733
+ yield path;
734
+ }
735
+ for (const [char, childNode] of node.children) {
736
+ yield* _dfs(childNode, path + char);
737
+ }
738
+ }
739
+
740
+ yield* _dfs(this.root, '');
741
+ }
742
+
743
+ /**
744
+ * (Protected) Normalize a string according to case sensitivity.
745
+ * @remarks Time O(L), Space O(L)
746
+ * @param str - Input string to normalize.
747
+ * @returns Normalized string based on caseSensitive.
748
+ */
749
+
750
+ protected _caseProcess(str: string) {
751
+ if (!this._caseSensitive) {
752
+ str = str.toLowerCase();
753
+ }
754
+ return str;
755
+ }
756
+ }