浅谈trie树(字典树)

一、引入

字典是干啥的?查找字的。

字典树自然也是起查找作用的。查找的是啥?单词。

看以下几个题:

1、给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单词表中出现过。

答:简单!map,短小精悍。

好。下一个

2、给出n个单词和m个询问,每次询问一个前缀,回答询问是多少个单词的前缀。

答:map,把每个单词拆开。

judge:n<=200000,TLE!

这就需要一种高级数据结构——Trie树(字典树)

 

二、原理

在本篇文章中,假设所有单词都只由小写字母构成

对cat,cash,app,apple,aply,ok 建一颗字典树,建成之后如下图所示

《浅谈trie树(字典树)》

由此可以看出:

1、字典树用边表示字母

2、有相同前缀的单词公用前缀节点,那我们可以的得出每个节点最多有26个子节点(在单词只包含小写字母的情况下)

3、整棵树的根节点是空的。为什么呢?便于插入和查找,这将会在后面解释。

4、每个单词结束的时候用一个特殊字符表示,图中用的‘’所经过的边的所有字母表示一个单词。

三、基本操作

A、insert,插入一个单词

1.思路

  从图中可以直观看出,从左到右扫这个单词,如果字母在相应根节点下没有出现过,就插入这个字母;否则沿着字典树往下走,看单词的下一个字母。

  这就产生一个问题:往哪儿插?计算机不会自己选择位置插,我们需要给它指定一个位置,那就需要给每个字母编号。

  我们设数组trie[i][j]=k,表示编号为i的节点的第j个孩子是编号为k的节点。

 什么意思呢?

 这里有2种编号,一种是i,k表示节点的位置编号,这是相对整棵树而言的;另一种是j,表示节点i的第j的孩子,这是相对节点i而言的。

 不理解?看图

 还是单词cat,cash,app,apple,aply,ok 

 我们就按输入顺序对其编第一种号,红色表示编号结果。因为先输入的cat,所以c,a,t分别是1,2,3,然后输入的是cash,因为c,a是公共前缀,所以从s开始编,s是4,以此类推。

注意这里相同字母的编号可能不同

 《浅谈trie树(字典树)》

 第二种编号,相对节点的编号,紫色表示编号结果。

因为每个节点最多有26个子节点,我们可以按他们的字典序从0——25编号,也就是他们的ASCLL码-a的ASCLL码。

注意这里相同字母的编号相同

《浅谈trie树(字典树)》

 实际上每个节点的子节点都应该从0编到——25,但这样会发现许多事根本用不到的。比如上图的根节点应该分出26个叉。节约空间,用到哪个分哪个。

 这样编号有什么用呢?

回到数组trie[i][j]=k。 数组trie[i][j]=k,表示编号为i的节点的第j个孩子是编号为k的节点。

那么第二种编号即为j,第一种编号即为i,k

 


 
  1. void insert()//插入单词s

  2. {

  3. len=strlen(s);//单词s的长度

  4. root=0;//根节点编号为0

  5. for(int i=0;i<len;i++)

  6. {

  7. int id=s[i]-'a';//第二种编号

  8. if(!trie[root][id])//如果之前没有从root到id的前缀

  9. trie[root][id]=++tot;//插入,tot即为第一种编号

  10. root=trie[root][id];//顺着字典树往下走

  11. }

  12. }

B、search,查找

查找有很多种,可以查找某一个前缀,也可以查找整个单词。

再次我们以查找一个前缀是否出现过为例讲解

1、思路

  从左往右以此扫描每个字母,顺着字典树往下找,能找到这个字母,往下走,否则结束查找,即没有这个前缀;前缀扫完了,表示有这个前缀。


 
  1. bool find()

  2. {

  3. len=strlen(s);

  4. root=0;//从根结点开始找

  5. for(int i=0;s[i];i++)

  6. {

  7. int x=s[i]-'a';//

  8. if(trie[root][x]==0) return false;//以root为头结点的x字母不存在,返回0

  9. root=trie[root][x];//为查询下个字母做准备,往下走

  10. }

  11. return true;//找到了

  12. }

3、如果是查询某个单词的话,我们用bool变量 v[i]表示节点i是否是单词结束的标志。

    那么最后return的是v[root],所以在插入操作中插入完每个单词是,要对单词最后一个字母的v[i]置为true,其他的都是false

4、如果是查询前缀出现的次数的话,那就在开一个sum[],表示位置i被访问过的次数,

   那么最后return的是sum[root],插入操作中每访问一个节点,都要让他的sum++

   这里前缀的次数是标记在前缀的最后一个字母所在位置的后一个位置上。

  比如:前缀abc出现的次数标记在c所在位置的后一个位置上,

 《浅谈trie树(字典树)》

四、完整代码

1、查询是否出现

 


 
  1. /* 数组模拟

  2. trie tree的储存方式:将字母储存在边上,边的节点连接与它相连的字母

  3. trie[rt][x]=tot:rt是上个节点编号,x是字母,tot是下个节点编号

  4. */

  5. #include<cstdio>

  6. #include<iostream>

  7. #include<algorithm>

  8. #include<cstring>

  9. #define maxn 2000010

  10. using namespace std;

  11. int tot=1,n;

  12. int trie[maxn][26];

  13. //bool isw[maxn];查询整个单词用

  14. void insert(char *s,int rt)

  15. {

  16. for(int i=0;s[i];i++)

  17. {

  18. int x=s[i]-'a';

  19. if(trie[rt][x]==0)//现在插入的字母在之前同一节点处未出现过

  20. {

  21. trie[rt][x]=++tot;//字母插入一个新的位置,否则不做处理

  22. }

  23. rt=trie[rt][x];//为下个字母的插入做准备

  24. }

  25. /*isw[rt]=true;标志该单词末位字母的尾结点,在查询整个单词时用到*/

  26. }

  27. bool find(char *s,int rt)

  28. {

  29. for(int i=0;s[i];i++)

  30. {

  31. int x=s[i]-'a';

  32. if(trie[rt][x]==0)return false;//以rt为头结点的x字母不存在,返回0

  33. rt=trie[rt][x];//为查询下个字母做准备

  34. }

  35. return true;

  36. //查询整个单词时,应该return isw[rt]

  37. }

  38. char s[22];

  39. int main()

  40. {

  41. tot=0;

  42. int rt=1;

  43. scanf("%d",&n);

  44. for(int i=1;i<=n;i++)

  45. {

  46. cin>>s;

  47. insert(s,rt);

  48. }

  49. scanf("%d",&n);

  50. for(int i=1;i<=n;i++)

  51. {

  52. cin>>s;

  53. if(find(s,rt))printf("YES\n");

  54. else printf("NO\n");

  55. }

  56. return 0;

  57. }

  58.  

2、查询前缀出现次数


 
  1. //数组模拟

  2. #include<iostream>

  3. #include<cstring>

  4. #include<cstdio>

  5. #include<algorithm>

  6. using namespace std;

  7. int trie[400001][26],len,root,tot,sum[400001];

  8. bool p;

  9. int n,m;

  10. char s[11];

  11. void insert()

  12. {

  13. len=strlen(s);

  14. root=0;

  15. for(int i=0;i<len;i++)

  16. {

  17. int id=s[i]-'a';

  18. if(!trie[root][id]) trie[root][id]=++tot;

  19. sum[trie[root][id]]++;//前缀后移一个位置保存

  20. root=trie[root][id];

  21. }

  22. }

  23. int search()

  24. {

  25. root=0;

  26. len=strlen(s);

  27. for(int i=0;i<len;i++)

  28. {

  29. int id=s[i]-'a';

  30. if(!trie[root][id]) return 0;

  31. root=trie[root][id];

  32. }//root经过此循环后变成前缀最后一个字母所在位置的后一个位置

  33. return sum[root];//因为前缀后移了一个保存,所以此时的sum[root]就是要求的前缀出现的次数

  34. }

  35. int main()

  36. {

  37. scanf("%d",&n);

  38. for(int i=1;i<=n;i++)

  39. {

  40. cin>>s;

  41. insert();

  42. }

  43. scanf("%d",&m);

  44. for(int i=1;i<=m;i++)

  45. {

  46. cin>s;

  47. printf("%d\n",search());

  48. }

  49. }

  50.  

 
  1. // 指针写法

  2. #include<cstdio>

  3. #include<cstring>

  4. #include<iostream>

  5. #include<algorithm>

  6. using namespace std;

  7. char s[11];

  8. int n,m;

  9. bool p;

  10. struct node

  11. {

  12. int count;

  13. node * next[26];

  14. }*root;

  15. node * build()

  16. {

  17. node * k=new(node);

  18. k->count=0;

  19. memset(k->next,0,sizeof(k->next));

  20. return k;

  21. }

  22. void insert()

  23. {

  24. node * r=root;

  25. char * word=s;

  26. while(*word)

  27. {

  28. int id=*word-'a';

  29. if(r->next[id]==NULL) r->next[id]=build();

  30. r=r->next[id];

  31. r->count++;

  32. word++;

  33. }

  34. }

  35. int search()

  36. {

  37. node * r=root;

  38. char * word=s;

  39. while(*word)

  40. {

  41. int id=*word-'a';

  42. r=r->next[id];

  43. if(r==NULL) return 0;

  44. word++;

  45. }

  46. return r->count;

  47. }

  48. int main()

  49. {

  50. root=build();

  51. scanf("%d",&n);

  52. for(int i=1;i<=n;i++)

  53. {

  54. cin>>s;

  55. insert();

  56. }

  57. scanf("%d",&m);

  58. for(int i=1;i<=m;i++)

  59. {

  60. cin>>s;

  61. printf("%d\n",search());

  62. }

  63. }

  64.  

五、模板题

hud 1251 统计难题 http://acm.hdu.edu.cn/showproblem.php?pid=1251

codevs 4189 字典 http://codevs.cn/problem/4189/

作者:xxy

出处:http://www.cnblogs.com/TheRoadToTheGold/

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
——————— 本文来自 initMyHeart 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u010982765/article/details/79759398?utm_source=copy

    原文作者:Trie树
    原文地址: https://blog.csdn.net/yangguang0012/article/details/82875825
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞