stack-typed 2.1.0 → 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 +552 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/esm/index.mjs +548 -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 +3 -3
  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/stack-typed.js +563 -0
  87. package/dist/umd/stack-typed.js.map +1 -0
  88. package/dist/umd/stack-typed.min.js +9 -0
  89. package/dist/umd/stack-typed.min.js.map +1 -0
  90. package/package.json +25 -5
  91. package/src/data-structures/binary-tree/avl-tree-counter.ts +8 -11
  92. package/src/data-structures/binary-tree/avl-tree-multi-map.ts +6 -11
  93. package/src/data-structures/binary-tree/avl-tree.ts +6 -8
  94. package/src/data-structures/binary-tree/binary-tree.ts +13 -15
  95. package/src/data-structures/binary-tree/bst.ts +6 -11
  96. package/src/data-structures/binary-tree/red-black-tree.ts +6 -11
  97. package/src/data-structures/binary-tree/tree-counter.ts +8 -13
  98. package/src/data-structures/binary-tree/tree-multi-map.ts +6 -11
  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 +3 -3
  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,610 +0,0 @@
1
- "use strict";
2
- /**
3
- * data-structure-typed
4
- *
5
- * @author Pablo Zeng
6
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
7
- * @license MIT License
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.Trie = exports.TrieNode = void 0;
11
- const base_1 = require("../base");
12
- /**
13
- * Node used by Trie to store one character and its children.
14
- * @remarks Time O(1), Space O(1)
15
- */
16
- 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
- constructor(key) {
23
- this._key = key;
24
- this._isEnd = false;
25
- this._children = new Map();
26
- }
27
- /**
28
- * Get the character key of this node.
29
- * @remarks Time O(1), Space O(1)
30
- * @returns Character key string.
31
- */
32
- get key() {
33
- return this._key;
34
- }
35
- /**
36
- * Set the character key of this node.
37
- * @remarks Time O(1), Space O(1)
38
- * @param value - New character key.
39
- * @returns void
40
- */
41
- set key(value) {
42
- this._key = value;
43
- }
44
- /**
45
- * Get the child map of this node.
46
- * @remarks Time O(1), Space O(1)
47
- * @returns Map from character to child node.
48
- */
49
- get children() {
50
- return this._children;
51
- }
52
- /**
53
- * Replace the child map of this node.
54
- * @remarks Time O(1), Space O(1)
55
- * @param value - New map of character → node.
56
- * @returns void
57
- */
58
- set children(value) {
59
- this._children = value;
60
- }
61
- /**
62
- * Check whether this node marks the end of a word.
63
- * @remarks Time O(1), Space O(1)
64
- * @returns True if this node ends a word.
65
- */
66
- get isEnd() {
67
- return this._isEnd;
68
- }
69
- /**
70
- * Mark this node as the end of a word or not.
71
- * @remarks Time O(1), Space O(1)
72
- * @param value - Whether this node ends a word.
73
- * @returns void
74
- */
75
- set isEnd(value) {
76
- this._isEnd = value;
77
- }
78
- }
79
- exports.TrieNode = TrieNode;
80
- /**
81
- * Prefix tree (Trie) for fast prefix queries and word storage.
82
- * @remarks Time O(1), Space O(1)
83
- * @template R
84
- * 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.
85
- * 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'.
86
- * 3. Fast Retrieval: Trie allows retrieval in O(m) time complexity, where m is the length of the string to be searched.
87
- * 4. Space Efficiency: Trie can store a large number of strings very space-efficiently, especially when these strings share common prefixes.
88
- * 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.
89
- * 6. Sorting: Trie can be used to sort a set of strings in alphabetical order.
90
- * 7. String Retrieval: For example, searching for a specific string in a large set of strings.
91
- * 8. Autocomplete: Providing recommended words or phrases as a user types.
92
- * 9. Spell Check: Checking the spelling of words.
93
- * 10. IP Routing: Used in certain types of IP routing algorithms.
94
- * 11. Text Word Frequency Count: Counting and storing the frequency of words in a large amount of text data.
95
- * @example
96
- * // Autocomplete: Prefix validation and checking
97
- * const autocomplete = new Trie<string>(['gmail.com', 'gmail.co.nz', 'gmail.co.jp', 'yahoo.com', 'outlook.com']);
98
- *
99
- * // Get all completions for a prefix
100
- * const gmailCompletions = autocomplete.getWords('gmail');
101
- * console.log(gmailCompletions); // ['gmail.com', 'gmail.co.nz', 'gmail.co.jp']
102
- * @example
103
- * // File System Path Operations
104
- * const fileSystem = new Trie<string>([
105
- * '/home/user/documents/file1.txt',
106
- * '/home/user/documents/file2.txt',
107
- * '/home/user/pictures/photo.jpg',
108
- * '/home/user/pictures/vacation/',
109
- * '/home/user/downloads'
110
- * ]);
111
- *
112
- * // Find common directory prefix
113
- * console.log(fileSystem.getLongestCommonPrefix()); // '/home/user/'
114
- *
115
- * // List all files in a directory
116
- * const documentsFiles = fileSystem.getWords('/home/user/documents/');
117
- * console.log(documentsFiles); // ['/home/user/documents/file1.txt', '/home/user/documents/file2.txt']
118
- * @example
119
- * // Autocomplete: Basic word suggestions
120
- * // Create a trie for autocomplete
121
- * const autocomplete = new Trie<string>([
122
- * 'function',
123
- * 'functional',
124
- * 'functions',
125
- * 'class',
126
- * 'classes',
127
- * 'classical',
128
- * 'closure',
129
- * 'const',
130
- * 'constructor'
131
- * ]);
132
- *
133
- * // Test autocomplete with different prefixes
134
- * console.log(autocomplete.getWords('fun')); // ['functional', 'functions', 'function']
135
- * console.log(autocomplete.getWords('cla')); // ['classes', 'classical', 'class']
136
- * console.log(autocomplete.getWords('con')); // ['constructor', 'const']
137
- *
138
- * // Test with non-matching prefix
139
- * console.log(autocomplete.getWords('xyz')); // []
140
- * @example
141
- * // Dictionary: Case-insensitive word lookup
142
- * // Create a case-insensitive dictionary
143
- * const dictionary = new Trie<string>([], { caseSensitive: false });
144
- *
145
- * // Add words with mixed casing
146
- * dictionary.add('Hello');
147
- * dictionary.add('WORLD');
148
- * dictionary.add('JavaScript');
149
- *
150
- * // Test lookups with different casings
151
- * console.log(dictionary.has('hello')); // true
152
- * console.log(dictionary.has('HELLO')); // true
153
- * console.log(dictionary.has('Hello')); // true
154
- * console.log(dictionary.has('javascript')); // true
155
- * console.log(dictionary.has('JAVASCRIPT')); // true
156
- * @example
157
- * // IP Address Routing Table
158
- * // Add IP address prefixes and their corresponding routes
159
- * const routes = {
160
- * '192.168.1': 'LAN_SUBNET_1',
161
- * '192.168.2': 'LAN_SUBNET_2',
162
- * '10.0.0': 'PRIVATE_NETWORK_1',
163
- * '10.0.1': 'PRIVATE_NETWORK_2'
164
- * };
165
- *
166
- * const ipRoutingTable = new Trie<string>(Object.keys(routes));
167
- *
168
- * // Check IP address prefix matching
169
- * console.log(ipRoutingTable.hasPrefix('192.168.1')); // true
170
- * console.log(ipRoutingTable.hasPrefix('192.168.2')); // true
171
- *
172
- * // Validate IP address belongs to subnet
173
- * const ip = '192.168.1.100';
174
- * const subnet = ip.split('.').slice(0, 3).join('.');
175
- * console.log(ipRoutingTable.hasPrefix(subnet)); // true
176
- */
177
- class Trie extends base_1.IterableElementBase {
178
- /**
179
- * Create a Trie and optionally bulk-insert words.
180
- * @remarks Time O(totalChars), Space O(totalChars)
181
- * @param [words] - Iterable of strings (or raw records if toElementFn is provided).
182
- * @param [options] - Options such as toElementFn and caseSensitive.
183
- * @returns New Trie instance.
184
- */
185
- constructor(words = [], options) {
186
- super(options);
187
- this._size = 0;
188
- this._caseSensitive = true;
189
- this._root = new TrieNode('');
190
- if (options) {
191
- const { caseSensitive } = options;
192
- if (caseSensitive !== undefined)
193
- this._caseSensitive = caseSensitive;
194
- }
195
- if (words) {
196
- this.addMany(words);
197
- }
198
- }
199
- /**
200
- * Get the number of stored words.
201
- * @remarks Time O(1), Space O(1)
202
- * @returns Word count.
203
- */
204
- get size() {
205
- return this._size;
206
- }
207
- /**
208
- * Get whether comparisons are case-sensitive.
209
- * @remarks Time O(1), Space O(1)
210
- * @returns True if case-sensitive.
211
- */
212
- get caseSensitive() {
213
- return this._caseSensitive;
214
- }
215
- /**
216
- * Get the root node.
217
- * @remarks Time O(1), Space O(1)
218
- * @returns Root TrieNode.
219
- */
220
- get root() {
221
- return this._root;
222
- }
223
- /**
224
- * (Protected) Get total count for base class iteration.
225
- * @remarks Time O(1), Space O(1)
226
- * @returns Total number of elements.
227
- */
228
- get _total() {
229
- return this._size;
230
- }
231
- /**
232
- * Insert one word into the trie.
233
- * @remarks Time O(L), Space O(L)
234
- * @param word - Word to insert.
235
- * @returns True if the word was newly added.
236
- */
237
- add(word) {
238
- word = this._caseProcess(word);
239
- let cur = this.root;
240
- let isNewWord = false;
241
- for (const c of word) {
242
- let nodeC = cur.children.get(c);
243
- if (!nodeC) {
244
- nodeC = new TrieNode(c);
245
- cur.children.set(c, nodeC);
246
- }
247
- cur = nodeC;
248
- }
249
- if (!cur.isEnd) {
250
- isNewWord = true;
251
- cur.isEnd = true;
252
- this._size++;
253
- }
254
- return isNewWord;
255
- }
256
- /**
257
- * Insert many words from an iterable.
258
- * @remarks Time O(ΣL), Space O(ΣL)
259
- * @param words - Iterable of strings (or raw records if toElementFn is provided).
260
- * @returns Array of per-word 'added' flags.
261
- */
262
- addMany(words) {
263
- const ans = [];
264
- for (const word of words) {
265
- if (this.toElementFn) {
266
- ans.push(this.add(this.toElementFn(word)));
267
- }
268
- else {
269
- ans.push(this.add(word));
270
- }
271
- }
272
- return ans;
273
- }
274
- /**
275
- * Check whether a word exists.
276
- * @remarks Time O(L), Space O(1)
277
- * @param word - Word to search for.
278
- * @returns True if present.
279
- */
280
- has(word) {
281
- word = this._caseProcess(word);
282
- let cur = this.root;
283
- for (const c of word) {
284
- const nodeC = cur.children.get(c);
285
- if (!nodeC)
286
- return false;
287
- cur = nodeC;
288
- }
289
- return cur.isEnd;
290
- }
291
- /**
292
- * Check whether the trie is empty.
293
- * @remarks Time O(1), Space O(1)
294
- * @returns True if size is 0.
295
- */
296
- isEmpty() {
297
- return this._size === 0;
298
- }
299
- /**
300
- * Remove all words and reset to a fresh root.
301
- * @remarks Time O(1), Space O(1)
302
- * @returns void
303
- */
304
- clear() {
305
- this._size = 0;
306
- this._root = new TrieNode('');
307
- }
308
- /**
309
- * Delete one word if present.
310
- * @remarks Time O(L), Space O(1)
311
- * @param word - Word to delete.
312
- * @returns True if a word was removed.
313
- */
314
- delete(word) {
315
- word = this._caseProcess(word);
316
- let isDeleted = false;
317
- const dfs = (cur, i) => {
318
- const char = word[i];
319
- const child = cur.children.get(char);
320
- if (child) {
321
- if (i === word.length - 1) {
322
- if (child.isEnd) {
323
- if (child.children.size > 0) {
324
- child.isEnd = false;
325
- }
326
- else {
327
- cur.children.delete(char);
328
- }
329
- isDeleted = true;
330
- return true;
331
- }
332
- return false;
333
- }
334
- const res = dfs(child, i + 1);
335
- if (res && !cur.isEnd && child.children.size === 0) {
336
- cur.children.delete(char);
337
- return true;
338
- }
339
- return false;
340
- }
341
- return false;
342
- };
343
- dfs(this.root, 0);
344
- if (isDeleted) {
345
- this._size--;
346
- }
347
- return isDeleted;
348
- }
349
- /**
350
- * Compute the height (max depth) of the trie.
351
- * @remarks Time O(N), Space O(H)
352
- * @returns Maximum depth from root to a leaf.
353
- */
354
- getHeight() {
355
- const startNode = this.root;
356
- let maxDepth = 0;
357
- if (startNode) {
358
- const bfs = (node, level) => {
359
- if (level > maxDepth) {
360
- maxDepth = level;
361
- }
362
- const { children } = node;
363
- if (children) {
364
- for (const child of children.entries()) {
365
- bfs(child[1], level + 1);
366
- }
367
- }
368
- };
369
- bfs(startNode, 0);
370
- }
371
- return maxDepth;
372
- }
373
- /**
374
- * Check whether input is a proper prefix of at least one word.
375
- * @remarks Time O(L), Space O(1)
376
- * @param input - String to test as prefix.
377
- * @returns True if input is a prefix but not a full word.
378
- */
379
- hasPurePrefix(input) {
380
- input = this._caseProcess(input);
381
- let cur = this.root;
382
- for (const c of input) {
383
- const nodeC = cur.children.get(c);
384
- if (!nodeC)
385
- return false;
386
- cur = nodeC;
387
- }
388
- return !cur.isEnd;
389
- }
390
- /**
391
- * Check whether any word starts with input.
392
- * @remarks Time O(L), Space O(1)
393
- * @param input - String to test as prefix.
394
- * @returns True if input matches a path from root.
395
- */
396
- hasPrefix(input) {
397
- input = this._caseProcess(input);
398
- let cur = this.root;
399
- for (const c of input) {
400
- const nodeC = cur.children.get(c);
401
- if (!nodeC)
402
- return false;
403
- cur = nodeC;
404
- }
405
- return true;
406
- }
407
- /**
408
- * Check whether the trie’s longest common prefix equals input.
409
- * @remarks Time O(min(H,L)), Space O(1)
410
- * @param input - Candidate longest common prefix.
411
- * @returns True if input equals the common prefix.
412
- */
413
- hasCommonPrefix(input) {
414
- input = this._caseProcess(input);
415
- let commonPre = '';
416
- const dfs = (cur) => {
417
- commonPre += cur.key;
418
- if (commonPre === input)
419
- return;
420
- if (cur.isEnd)
421
- return;
422
- if (cur && cur.children && cur.children.size === 1)
423
- dfs(Array.from(cur.children.values())[0]);
424
- else
425
- return;
426
- };
427
- dfs(this.root);
428
- return commonPre === input;
429
- }
430
- /**
431
- * Return the longest common prefix among all words.
432
- * @remarks Time O(H), Space O(1)
433
- * @returns The longest common prefix string.
434
- */
435
- getLongestCommonPrefix() {
436
- let commonPre = '';
437
- const dfs = (cur) => {
438
- commonPre += cur.key;
439
- if (cur.isEnd)
440
- return;
441
- if (cur && cur.children && cur.children.size === 1)
442
- dfs(Array.from(cur.children.values())[0]);
443
- else
444
- return;
445
- };
446
- dfs(this.root);
447
- return commonPre;
448
- }
449
- /**
450
- * Collect words under a prefix up to a maximum count.
451
- * @remarks Time O(K·L), Space O(K·L)
452
- * @param [prefix] - Prefix to match; default empty string for root.
453
- * @param [max] - Maximum number of words to return; default is Number.MAX_SAFE_INTEGER.
454
- * @param [isAllWhenEmptyPrefix] - When true, collect from root even if prefix is empty.
455
- * @returns Array of collected words (at most max).
456
- */
457
- getWords(prefix = '', max = Number.MAX_SAFE_INTEGER, isAllWhenEmptyPrefix = false) {
458
- prefix = this._caseProcess(prefix);
459
- const words = [];
460
- let found = 0;
461
- function dfs(node, word) {
462
- for (const char of node.children.keys()) {
463
- const charNode = node.children.get(char);
464
- if (charNode !== undefined) {
465
- dfs(charNode, word.concat(char));
466
- }
467
- }
468
- if (node.isEnd) {
469
- if (found > max - 1)
470
- return;
471
- words.push(word);
472
- found++;
473
- }
474
- }
475
- let startNode = this.root;
476
- if (prefix) {
477
- for (const c of prefix) {
478
- const nodeC = startNode.children.get(c);
479
- if (nodeC) {
480
- startNode = nodeC;
481
- }
482
- else {
483
- return [];
484
- }
485
- }
486
- }
487
- if (isAllWhenEmptyPrefix || startNode !== this.root)
488
- dfs(startNode, prefix);
489
- return words;
490
- }
491
- /**
492
- * Deep clone this trie by iterating and inserting all words.
493
- * @remarks Time O(ΣL), Space O(ΣL)
494
- * @returns A new trie with the same words and options.
495
- */
496
- clone() {
497
- const next = this._createInstance();
498
- for (const x of this)
499
- next.add(x);
500
- return next;
501
- }
502
- /**
503
- * Filter words into a new trie of the same class.
504
- * @remarks Time O(ΣL), Space O(ΣL)
505
- * @param predicate - Predicate (word, index, trie) → boolean to keep word.
506
- * @param [thisArg] - Value for `this` inside the predicate.
507
- * @returns A new trie containing words that satisfy the predicate.
508
- */
509
- filter(predicate, thisArg) {
510
- const results = this._createInstance();
511
- let index = 0;
512
- for (const word of this) {
513
- if (predicate.call(thisArg, word, index, this)) {
514
- results.add(word);
515
- }
516
- index++;
517
- }
518
- return results;
519
- }
520
- map(callback, options, thisArg) {
521
- const newTrie = this._createLike([], options);
522
- let i = 0;
523
- for (const x of this) {
524
- const v = thisArg === undefined ? callback(x, i++, this) : callback.call(thisArg, x, i++, this);
525
- if (typeof v !== 'string') {
526
- throw new TypeError(`Trie.map callback must return string; got ${typeof v}`);
527
- }
528
- newTrie.add(v);
529
- }
530
- return newTrie;
531
- }
532
- /**
533
- * Map words into a new trie of the same element type.
534
- * @remarks Time O(ΣL), Space O(ΣL)
535
- * @param callback - Mapping function (word, index, trie) → string.
536
- * @param [thisArg] - Value for `this` inside the callback.
537
- * @returns A new trie with mapped words.
538
- */
539
- mapSame(callback, thisArg) {
540
- const next = this._createInstance();
541
- let i = 0;
542
- for (const key of this) {
543
- const mapped = thisArg === undefined ? callback(key, i++, this) : callback.call(thisArg, key, i++, this);
544
- next.add(mapped);
545
- }
546
- return next;
547
- }
548
- /**
549
- * (Protected) Create an empty instance of the same concrete class.
550
- * @remarks Time O(1), Space O(1)
551
- * @param [options] - Options forwarded to the constructor.
552
- * @returns An empty like-kind trie instance.
553
- */
554
- _createInstance(options) {
555
- const Ctor = this.constructor;
556
- const next = new Ctor([], Object.assign({ toElementFn: this.toElementFn, caseSensitive: this.caseSensitive }, (options !== null && options !== void 0 ? options : {})));
557
- return next;
558
- }
559
- /**
560
- * (Protected) Create a like-kind trie and seed it from an iterable.
561
- * @remarks Time O(ΣL), Space O(ΣL)
562
- * @template RM
563
- * @param [elements] - Iterable used to seed the new trie.
564
- * @param [options] - Options forwarded to the constructor.
565
- * @returns A like-kind Trie instance.
566
- */
567
- _createLike(elements = [], options) {
568
- const Ctor = this.constructor;
569
- return new Ctor(elements, options);
570
- }
571
- /**
572
- * (Protected) Spawn an empty like-kind trie instance.
573
- * @remarks Time O(1), Space O(1)
574
- * @template RM
575
- * @param [options] - Options forwarded to the constructor.
576
- * @returns An empty like-kind Trie instance.
577
- */
578
- _spawnLike(options) {
579
- return this._createLike([], options);
580
- }
581
- /**
582
- * (Protected) Iterate all words in lexicographic order of edges.
583
- * @remarks Time O(ΣL), Space O(H)
584
- * @returns Iterator of words.
585
- */
586
- *_getIterator() {
587
- function* _dfs(node, path) {
588
- if (node.isEnd) {
589
- yield path;
590
- }
591
- for (const [char, childNode] of node.children) {
592
- yield* _dfs(childNode, path + char);
593
- }
594
- }
595
- yield* _dfs(this.root, '');
596
- }
597
- /**
598
- * (Protected) Normalize a string according to case sensitivity.
599
- * @remarks Time O(L), Space O(L)
600
- * @param str - Input string to normalize.
601
- * @returns Normalized string based on caseSensitive.
602
- */
603
- _caseProcess(str) {
604
- if (!this._caseSensitive) {
605
- str = str.toLowerCase();
606
- }
607
- return str;
608
- }
609
- }
610
- exports.Trie = Trie;
package/dist/index.d.ts DELETED
@@ -1,12 +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
- export * from './data-structures/stack';
9
- export * from './types/data-structures/stack';
10
- export * from './types/common';
11
- export * from './types/utils';
12
- export * from './common';
package/dist/index.js DELETED
@@ -1,28 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- /**
18
- * data-structure-typed
19
- *
20
- * @author Pablo Zeng
21
- * @copyright Copyright (c) 2022 Pablo Zeng <zrwusa@gmail.com>
22
- * @license MIT License
23
- */
24
- __exportStar(require("./data-structures/stack"), exports);
25
- __exportStar(require("./types/data-structures/stack"), exports);
26
- __exportStar(require("./types/common"), exports);
27
- __exportStar(require("./types/utils"), exports);
28
- __exportStar(require("./common"), exports);
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,24 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./graph"), exports);
18
- __exportStar(require("./binary-tree"), exports);
19
- __exportStar(require("./doubly-linked-list"), exports);
20
- __exportStar(require("./heap"), exports);
21
- __exportStar(require("./navigator"), exports);
22
- __exportStar(require("./priority-queue"), exports);
23
- __exportStar(require("./segment-tree"), exports);
24
- __exportStar(require("./singly-linked-list"), exports);
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });