zoukankan      html  css  js  c++  java
  • 字典树的建立和基本查找

    字典树的建立和基本查找

    一.字典树的定义

    字典树又叫做前缀树,任意一个或多个字符串可以构建一棵字典树用于存储多个串的公共前缀

    二.构建字典树的两种方法

    (1)字典树的链表构建及查找

    在用链表构造的字典树中每一个节点有着一个数据域来存放该点代表的字符和26个指针分别指向a(A)~z(Z)26个可能出现的子字符,在寻找某个字符串是否出现时,从根节点出发不断向下查找配对,如果到了最后一个字符都存在则表示该前缀存在

    (2)字典树的二维数组构建及查找

    用二维数组tree[i][j]来标识一颗字典树,其中i为父节点,j为子节点,程序开始时父节点为root节点。tree[i][j]表示节点i第j个儿子的编号【这里我们规定root的编号为0】

    1) 字典树的二维数组构建

    2)字典树的二维数组查找

    三.代码模板

    (1) 字典树的链表模板

     1 #include<iostream>
     2 #include<cstring>
     3 using namespace std;
     4  
     5 const int SIZE=10; 
     6 struct Trie
     7 {
     8     int count;//前缀出现的次数
     9     struct Trie *next[SIZE];//孩子节点的数目 
    10     bool isEnd; //判断到这个位置是否是一个单词
    11     string name; 
    12     Trie()//构造函数 
    13     {
    14         count=0;
    15         memset(next,0,sizeof(next));
    16         isEnd=false;
    17     } 
    18 };  
    19 struct Trie *root=new Trie;//建立根节点
    20 void Insert(string s)
    21 {
    22     int len=s.length();
    23     int pos;
    24     struct Trie *u=root;
    25     for(int i=0;i<len;i++)
    26     {
    27         pos=s[i]-'0';
    28         if(u->next[pos]==NULL)//数字在边上,或者说是以位置的方式体现,不需要存储 
    29             u->next[pos]=new Trie;
    30         u=u->next[pos];
    31         u->count++;        
    32     }
    33     u->isEnd=true;//表明为一个单词的节点
    34     u->name=s;//同时存储单词 
    35 } 
    36 int Search(string s)
    37 {
    38     struct Trie *u=root;
    39     int len=s.length();
    40     for(int i=0;i<len;i++)
    41     {
    42         int pos=s[i]-'0';
    43         if(u->next[pos]==NULL)
    44             return 0;
    45         else
    46             u=u->next[pos];
    47     }
    48     return u->count;
    49 } 
    50 void del(struct Trie *u)
    51 {
    52     for(int i=0;i<SIZE;i++)
    53     {
    54         if(u->next[i]!=NULL)
    55             del(u->next[i]);
    56     }
    57     delete(u);
    58 }
    59 void print(struct Trie *u)
    60 {
    61     if(u->isEnd)
    62         cout<<u->name<<":"<<u->count<<endl;
    63     for(int i=0;i<SIZE;i++)
    64         if(u->next[i]!=NULL)
    65             print(u->next[i]); 
    66 }
    67  
    68 int main()
    69 {
    70     int n;
    71     string s;
    72     cin>>n;
    73     while(n--)
    74     {
    75         cin>>s;
    76         Insert(s);
    77     }
    78     print(root);//打印检查下 
    79     del(root);//释放树,下次重新建立 
    80     return 0;
    81 } 

    (2) 字典树的二维数组模板

     1 const int maxn =2e6+5;//如果是64MB可以开到2e6+5,尽量开大
     2 int tree[maxn][30];//tree[i][j]表示节点i的第j个儿子的节点编号
     3 bool flagg[maxn];//表示以该节点结尾是一个单词
     4 int tot;//总节点数
     5 void insert_(char *str)
     6 {
     7    int  len=strlen(str);
     8    int root=0;
     9    for(int i=0;i<len;i++)
    10    {
    11        int id=str[i]-'0';
    12        if(!tree[root][id]) tree[root][id]=++tot;
    13        root=tree[root][id];
    14    }
    15    flagg[root]=true;
    16 }
    17 bool find_(char *str)//查询操作,按具体要求改动
    18 {
    19     int len=strlen(str);
    20     int root=0;
    21     for(int i=0;i<len;i++)
    22     {
    23         int id=str[i]-'0';
    24         if(!tree[root][id]) return false;
    25         root=tree[root][id];
    26     }
    27     return true;
    28 }
    29 void init()//最后清空,节省时间
    30 {
    31     for(int i=0;i<=tot;i++)
    32     {
    33        flagg[i]=false;
    34        for(int j=0;j<10;j++)
    35            tree[i][j]=0;
    36     }
    37    tot=0;
    38 }

     

  • 相关阅读:
    杭电2063 过山车 匈牙利算法
    杭电2023 平均成绩
    leveldb性能分析
    linux下libreoffice安装测试
    iptables配置vsftp访问
    vsftp访问异常
    mysql二进制安装
    vi命令
    mysql配置优化
    rsync 配置
  • 原文地址:https://www.cnblogs.com/PokimonMaster/p/12188791.html
Copyright © 2011-2022 走看看