For example we have an array of words:

  1. [car, done, try, cat, trie, do]

What is the best data structure to store the data and easy for search?

We can use Trie data structure, it is a tree, but not a binary tree. The reuslts of constructing a tree by using the example array data.

True of False means whether this letter is the last of the word.

We can code it by Javascript:

  • We are using Map data structure
  • If there is no existing node for giving letter, we create a new Node.
  • If there is a existing node, then we continue to next letter
  1. function Node() {
  2. return {
  3. keys: new Map(),
  4. isWord: false
  5. };
  6. }
  7.  
  8. function BuildTrie() {
  9. const root = new Node();
  10. return {
  11. root,
  12. add(word, node = this.root) {
  13. if (word.length === 0) {
  14. node.isWord = true;
  15. return;
  16. }
  17. const [head, ...rest] = word;
  18. if (!node.keys.has(head)) {
  19. node.keys.set(head, new Node());
  20. }
  21.  
  22. return this.add(rest, node.keys.get(head));
  23. },
  24. hasWord(word, node = this.root) {
  25. if (!word) {
  26. return false;
  27. }
  28. while (word.length > 1) {
  29. if (!node.keys.has(word[0])) {
  30. return false;
  31. } else {
  32. node = node.keys.get(word[0]);
  33. word = word.substr(1);
  34. }
  35. }
  36. return node.keys.has(word) && node.keys.get(word).isWord;
  37. },
  38. print() {
  39. let words = [];
  40. function search(node = this.root, string = "") {
  41. if (node.keys.size != 0) {
  42. for (let letter of node.keys.keys()) {
  43. search(node.keys.get(letter), string.concat(letter));
  44. }
  45. if (node.isWord) {
  46. words.push(string);
  47. }
  48. } else {
  49. string.length > 0 ? words.push(string) : undefined;
  50. }
  51. }
  52.  
  53. search(this.root, "");
  54. return words.length > 0 ? words : null;
  55. }
  56. };
  57. }
  58.  
  59. const t = new BuildTrie();
  60.  
  61. t.add("cat");
  62. t.add("cal");
  63.  
  64. console.log(t.hasWord("cat")); // true
  65. console.log(t.hasWord("catt")); // false

  

[Algorithm] Trie data structure的更多相关文章

  1. Summary: Trie Data Structure

    Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...

  2. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  3. (Data structure)Implement Trie && Add and Search Word

    Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith methods. Note:You ...

  4. leetcode 211. Add and Search Word - Data structure design Trie树

    题目链接 写一个数据结构, 支持两种操作. 加入一个字符串, 查找一个字符串是否存在.查找的时候, '.'可以代表任意一个字符. 显然是Trie树, 添加就是正常的添加, 查找的时候只要dfs查找就可 ...

  5. 字典树(查找树) leetcode 208. Implement Trie (Prefix Tree) 、211. Add and Search Word - Data structure design

    字典树(查找树) 26个分支作用:检测字符串是否在这个字典里面插入.查找 字典树与哈希表的对比:时间复杂度:以字符来看:O(N).O(N) 以字符串来看:O(1).O(1)空间复杂度:字典树远远小于哈 ...

  6. LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design

    字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith  ...

  7. [leetcode trie]211. Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  8. [Algorithm] JavaScript Graph Data Structure

    A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. ...

  9. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. CentOS系统php5.6安装ImageMagick处理webp格式图片

    1.先安装webp yum install libwebp 2.编译安装ImageMagick 之前有过yum安装的先卸载 yum remove ImageMagick 我使用的是老版本ImageMa ...

  2. lamp 5.6.36 bug记录

    后来发现另一个问题,php文字水印中文是乱码. 用yum安装lamp环境详见:https://blog.csdn.net/u010071211/article/details/80370201 在ce ...

  3. 4040 EZ系列之奖金

    4040 EZ系列之奖金 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond   题目描述 Description 由于无敌的WRN在2015年世界英俊帅气男总决选中 ...

  4. [POI2015]Łasuchy

    [POI2015]Łasuchy 题目大意: 圆桌上摆放着\(n(n\le10^6)\)份食物,围成一圈,第\(i\)份食物所含热量为\(c_i\). 相邻两份食物之间坐着一个人,共有\(n\)个人. ...

  5. Codeforces Round #281 (Div. 2) A. Vasya and Football 暴力水题

    A. Vasya and Football time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. 关于Vue的一些小技巧

    前言 用Vue开发一个网页并不难,但是也经常会遇到一些问题,其实大部分的问题都在文档中有所提及,再不然我们通过谷歌也能成功搜索到问题的答案,为了帮助小伙伴们提前踩坑,在遇到问题的时候,心里大概有个谱知 ...

  7. CentOS 7提示:ERROR unsupported format character '(0xffffffe7) at/域安装失败,您可以运行下列命令重启您的域:

    别理会,直接装即可,这个错误不影响使用.

  8. [Dynamic Language] Python定时任务框架

    APScheduler是一个Python定时任务框架,使用起来十分方便.提供了基于日期.固定时间间隔以及crontab类型的任务,并且可以持久化任务.并以daemon方式运行应用. 在APSchedu ...

  9. java对mongodb的and, in, or 经常使用操作

    DBCollection dbcon = null; DBObject query = new BasicDBObject(); BasicDBList values = new BasicDBLis ...

  10. SSH深度历险(四) Maven初步学�

    这几天接触这个词,非常多遍了,仅仅是浅显的体会到它在GXPT中的优点,功能之强大,又通过网络查询了资料进一步的认识学习了,和大家分享. Maven是基于项目对象模型(POM),能够通过一小段描写叙述信 ...