trie.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * Suffix Trie based on Mnemonist Trie
  3. */
  4. // import { Trie } from 'mnemonist';
  5. export const SENTINEL = Symbol('SENTINEL');
  6. type TrieNode = {
  7. [SENTINEL]: boolean,
  8. [Bun.inspect.custom]: () => string
  9. } & Map<string, TrieNode>;
  10. const deepTrieNodeToJSON = (node: TrieNode) => {
  11. const obj: Record<string, any> = {};
  12. if (node[SENTINEL]) {
  13. obj['[start]'] = node[SENTINEL];
  14. }
  15. node.forEach((value, key) => {
  16. obj[key] = deepTrieNodeToJSON(value);
  17. });
  18. return obj;
  19. };
  20. const createNode = (): TrieNode => {
  21. const node = new Map<string, TrieNode>() as TrieNode;
  22. node[SENTINEL] = false;
  23. node[Bun.inspect.custom] = () => JSON.stringify(deepTrieNodeToJSON(node), null, 2);
  24. return node;
  25. };
  26. export const createTrie = (from?: string[] | Set<string> | null) => {
  27. let size = 0;
  28. const root: TrieNode = createNode();
  29. /**
  30. * Method used to add the given prefix to the trie.
  31. */
  32. const add = (suffix: string): void => {
  33. let node: TrieNode = root;
  34. let token: string;
  35. for (let i = suffix.length - 1; i >= 0; i--) {
  36. token = suffix[i];
  37. if (node.has(token)) {
  38. node = node.get(token)!;
  39. } else {
  40. const newNode = createNode();
  41. node.set(token, newNode);
  42. node = newNode;
  43. }
  44. }
  45. // Do we need to increase size?
  46. if (!node[SENTINEL]) {
  47. size++;
  48. }
  49. node[SENTINEL] = true;
  50. };
  51. /**
  52. * @param {string} suffix
  53. */
  54. const contains = (suffix: string): boolean => {
  55. let node: TrieNode | undefined = root;
  56. let token: string;
  57. for (let i = suffix.length - 1; i >= 0; i--) {
  58. token = suffix[i];
  59. node = node.get(token);
  60. if (!node) {
  61. return false;
  62. }
  63. }
  64. return true;
  65. };
  66. /**
  67. * Method used to retrieve every item in the trie with the given prefix.
  68. */
  69. const find = (inputSuffix: string, /** @default true */ includeEqualWithSuffix = true): string[] => {
  70. let node: TrieNode | undefined = root;
  71. let token: string;
  72. for (let i = inputSuffix.length - 1; i >= 0; i--) {
  73. token = inputSuffix[i];
  74. node = node.get(token);
  75. if (!node) {
  76. return [];
  77. }
  78. }
  79. const matches: string[] = [];
  80. // Performing DFS from prefix
  81. const nodeStack: TrieNode[] = [node];
  82. const suffixStack: string[] = [inputSuffix];
  83. do {
  84. const suffix: string = suffixStack.pop()!;
  85. node = nodeStack.pop()!;
  86. if (node[SENTINEL]) {
  87. if (includeEqualWithSuffix || suffix !== inputSuffix) {
  88. matches.push(suffix);
  89. }
  90. }
  91. node.forEach((childNode, k) => {
  92. nodeStack.push(childNode);
  93. suffixStack.push(k + suffix);
  94. });
  95. } while (nodeStack.length);
  96. return matches;
  97. };
  98. /**
  99. * Works like trie.find, but instead of returning the matches as an array, it removes them from the given set in-place.
  100. */
  101. const substractSetInPlaceFromFound = (inputSuffix: string, set: Set<string>) => {
  102. let node: TrieNode | undefined = root;
  103. let token: string;
  104. // Find the leaf-est node, and early return if not any
  105. for (let i = inputSuffix.length - 1; i >= 0; i--) {
  106. token = inputSuffix[i];
  107. node = node.get(token);
  108. if (!node) {
  109. return;
  110. }
  111. }
  112. // Performing DFS from prefix
  113. const nodeStack: TrieNode[] = [node];
  114. const suffixStack: string[] = [inputSuffix];
  115. do {
  116. const suffix = suffixStack.pop()!;
  117. node = nodeStack.pop()!;
  118. if (node[SENTINEL]) {
  119. if (suffix !== inputSuffix) {
  120. // found match, delete it from set
  121. set.delete(suffix);
  122. }
  123. }
  124. node.forEach((childNode, k) => {
  125. nodeStack.push(childNode);
  126. suffixStack.push(k + suffix);
  127. });
  128. } while (nodeStack.length);
  129. };
  130. /**
  131. * Method used to delete a prefix from the trie.
  132. */
  133. const remove = (suffix: string): boolean => {
  134. let node: TrieNode | undefined = root;
  135. let toPrune: TrieNode | null = null;
  136. let tokenToPrune: string | null = null;
  137. let parent: TrieNode = node;
  138. let token: string;
  139. for (let i = suffix.length - 1; i >= 0; i--) {
  140. token = suffix[i];
  141. parent = node;
  142. node = node.get(token);
  143. if (!node) {
  144. return false;
  145. }
  146. // Keeping track of a potential branch to prune
  147. // If the node is to be pruned, but they are more than one token child in it, we can't prune it
  148. // If there is only one token child, or no child at all, we can prune it safely
  149. const onlyChild = node.size === 1 && node.has(token);
  150. if (onlyChild) {
  151. toPrune = parent;
  152. tokenToPrune = token;
  153. } else if (toPrune !== null) { // not only child, retain the branch
  154. toPrune = null;
  155. tokenToPrune = null;
  156. }
  157. }
  158. if (!node[SENTINEL]) return false;
  159. size--;
  160. if (tokenToPrune && toPrune) {
  161. toPrune.delete(tokenToPrune);
  162. } else {
  163. node[SENTINEL] = false;
  164. }
  165. return true;
  166. };
  167. /**
  168. * Method used to assert whether the given prefix exists in the Trie.
  169. */
  170. const has = (suffix: string): boolean => {
  171. let node: TrieNode = root;
  172. for (let i = suffix.length - 1; i >= 0; i--) {
  173. const token = suffix[i];
  174. if (node.has(token)) {
  175. node = node.get(token)!;
  176. } else {
  177. return false;
  178. }
  179. }
  180. return node[SENTINEL];
  181. };
  182. if (Array.isArray(from)) {
  183. for (let i = 0, l = from.length; i < l; i++) {
  184. add(from[i]);
  185. }
  186. } else if (from) {
  187. from.forEach(add);
  188. }
  189. const dump = () => {
  190. const node = root;
  191. const nodeStack: TrieNode[] = [];
  192. const suffixStack: string[] = [];
  193. // Resolving initial string
  194. const suffix = '';
  195. nodeStack.push(node);
  196. suffixStack.push(suffix);
  197. const results: string[] = [];
  198. let currentNode: TrieNode;
  199. let currentPrefix: string;
  200. let hasValue = false;
  201. do {
  202. currentNode = nodeStack.pop()!;
  203. currentPrefix = suffixStack.pop()!;
  204. if (currentNode[SENTINEL]) {
  205. hasValue = true;
  206. }
  207. node.forEach((childNode, k) => {
  208. nodeStack.push(childNode);
  209. suffixStack.push(k + suffix);
  210. });
  211. if (hasValue) results.push(currentPrefix);
  212. } while (nodeStack.length);
  213. return results;
  214. };
  215. return {
  216. add,
  217. contains,
  218. find,
  219. substractSetInPlaceFromFound,
  220. remove,
  221. delete: remove,
  222. has,
  223. dump,
  224. get size() {
  225. return size;
  226. },
  227. get root() {
  228. return root;
  229. },
  230. [Bun.inspect.custom]: () => JSON.stringify(deepTrieNodeToJSON(root), null, 2)
  231. };
  232. };
  233. export default createTrie;