trie.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Suffix Trie based on Mnemonist Trie
  3. */
  4. const SENTINEL = String.fromCharCode(0);
  5. class Trie {
  6. size = 0;
  7. root = {};
  8. /**
  9. * Method used to add the given prefix to the trie.
  10. *
  11. * @param {string} suffix - Prefix to follow.
  12. * @return {Trie}
  13. */
  14. add(suffix) {
  15. let node = this.root;
  16. let token;
  17. for (let i = suffix.length - 1; i >= 0; i--) {
  18. token = suffix[i];
  19. node = node[token] || (node[token] = {});
  20. }
  21. // Do we need to increase size?
  22. if (!(SENTINEL in node)) this.size++;
  23. node[SENTINEL] = true;
  24. return this;
  25. }
  26. /**
  27. * @param {string} suffix
  28. */
  29. contains(suffix) {
  30. let node = this.root;
  31. let token;
  32. for (let i = suffix.length - 1; i >= 0; i--) {
  33. token = suffix[i];
  34. node = node[token];
  35. if (node == null) return false;
  36. }
  37. return true;
  38. }
  39. /**
  40. * Method used to retrieve every item in the trie with the given prefix.
  41. *
  42. * @param {string} suffix - Prefix to query.
  43. * @param {boolean} [includeEqualWithSuffix]
  44. * @return {string[]}
  45. */
  46. find(suffix, includeEqualWithSuffix = true) {
  47. let node = this.root;
  48. const matches = [];
  49. let token;
  50. for (let i = suffix.length - 1; i >= 0; i--) {
  51. token = suffix[i];
  52. node = node[token];
  53. if (node == null) return matches;
  54. }
  55. // Performing DFS from prefix
  56. const nodeStack = [node];
  57. const suffixStack = [suffix];
  58. let k;
  59. let $suffix = suffix;
  60. while (nodeStack.length) {
  61. $suffix = suffixStack.pop();
  62. node = nodeStack.pop();
  63. for (k in node) {
  64. if (k === SENTINEL) {
  65. if (includeEqualWithSuffix) {
  66. matches.push($suffix);
  67. } else if ($suffix !== suffix) {
  68. matches.push($suffix);
  69. }
  70. continue;
  71. }
  72. nodeStack.push(node[k]);
  73. suffixStack.push(k + $suffix);
  74. }
  75. }
  76. return matches;
  77. }
  78. toJSON() {
  79. return this.root;
  80. }
  81. /**
  82. * Method used to clear the trie.
  83. *
  84. * @return {void}
  85. */
  86. // clear() {
  87. // // Properties
  88. // this.root = {};
  89. // this.size = 0;
  90. // }
  91. /**
  92. * Method used to update the value of the given prefix in the trie.
  93. *
  94. * @param {string|array} prefix - Prefix to follow.
  95. * @param {(oldValue: any | undefined) => any} updateFunction - Update value visitor callback.
  96. * @return {Trie}
  97. */
  98. // update(prefix, updateFunction) {
  99. // let node = this.root;
  100. // let token;
  101. // for (let i = 0, l = prefix.length; i < l; i++) {
  102. // token = prefix[i];
  103. // node = node[token] || (node[token] = {});
  104. // }
  105. // // Do we need to increase size?
  106. // if (!(SENTINEL in node))
  107. // this.size++;
  108. // node[SENTINEL] = updateFunction(node[SENTINEL]);
  109. // return this;
  110. // }
  111. /**
  112. * Method used to delete a prefix from the trie.
  113. *
  114. * @param {string} suffix - Prefix to delete.
  115. * @return {boolean}
  116. */
  117. delete(suffix) {
  118. let node = this.root;
  119. let toPrune = null;
  120. let tokenToPrune = null;
  121. let parent;
  122. let token;
  123. for (let i = suffix.length - 1; i >= 0; i--) {
  124. token = suffix[i];
  125. parent = node;
  126. node = node[token];
  127. // Prefix does not exist
  128. if (typeof node === 'undefined')
  129. return false;
  130. // Keeping track of a potential branch to prune
  131. if (toPrune !== null) {
  132. if (Object.keys(node).length > 1) {
  133. toPrune = null;
  134. tokenToPrune = null;
  135. }
  136. }
  137. else {
  138. if (Object.keys(node).length < 2) {
  139. toPrune = parent;
  140. tokenToPrune = token;
  141. }
  142. }
  143. }
  144. if (!(SENTINEL in node)) return false;
  145. this.size--;
  146. if (toPrune) {
  147. delete toPrune[tokenToPrune];
  148. } else {
  149. delete node[SENTINEL];
  150. }
  151. return true;
  152. }
  153. /**
  154. * Method used to assert whether the given prefix exists in the Trie.
  155. *
  156. * @param {string} suffix - Prefix to check.
  157. * @return {boolean}
  158. */
  159. has(suffix) {
  160. let node = this.root;
  161. let token;
  162. for (let i = suffix.length - 1; i >= 0; i--) {
  163. token = suffix[i];
  164. node = node[token];
  165. if (typeof node === 'undefined')
  166. return false;
  167. }
  168. return SENTINEL in node;
  169. }
  170. /**
  171. * @return {string[]}
  172. */
  173. dump() {
  174. let node = this.root;
  175. const nodeStack = [];
  176. const prefixStack = [];
  177. // Resolving initial prefix
  178. const prefix = '';
  179. nodeStack.push(node);
  180. prefixStack.push(prefix);
  181. /** @type {string[]} */
  182. const results = [];
  183. let currentNode;
  184. let currentPrefix;
  185. let hasValue = false;
  186. let k;
  187. while (nodeStack.length) {
  188. currentNode = nodeStack.pop();
  189. currentPrefix = prefixStack.pop();
  190. for (k in currentNode) {
  191. if (k === SENTINEL) {
  192. hasValue = true;
  193. continue;
  194. }
  195. nodeStack.push(currentNode[k]);
  196. prefixStack.push(k + currentPrefix);
  197. }
  198. if (hasValue) results.push(currentPrefix);
  199. }
  200. return results;
  201. }
  202. /**
  203. * Convenience known methods.
  204. */
  205. // inspect() {
  206. // const proxy = new Set();
  207. // const iterator = this.prefixes();
  208. // let step;
  209. // while ((step = iterator.next(), !step.done))
  210. // proxy.add(step.value);
  211. // // Trick so that node displays the name of the constructor
  212. // Object.defineProperty(proxy, 'constructor', {
  213. // value: Trie,
  214. // enumerable: false
  215. // });
  216. // return proxy;
  217. // }
  218. /**
  219. * Static .from function taking an arbitrary iterable & converting it into
  220. * a trie.
  221. *
  222. * @param {string[]} iterable - Target iterable.
  223. * @return {Trie}
  224. */
  225. static from = iterable => {
  226. const trie = new Trie();
  227. iterable.forEach(i => trie.add(i));
  228. return trie;
  229. };
  230. }
  231. /**
  232. * Exporting.
  233. */
  234. module.exports.SENTINEL = SENTINEL;
  235. module.exports = Trie;