成功源于积累----波爸

思想决定高度,行动决定成败!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
昨天在北邮人bbs上逛时,看到有人在讨论字符窜提示的问题,有人回帖用trie树来做,而我以前也听过trie树,不过想系统学习下,于是大概用了一个下午的时间来学习trie树。网上有很多关于trie树的资料,其核心思想就时拿空间换取时间,很适合搜索前缀子窜,时间效率比较高,主要是占用空间比较大。为什么会这样呢?看下trie树的结构就知道拉。
如图所示,该trie树存有abc、d、da、dda四个字符串,如果是字符串会在节点的尾部进行标记。没有后续字符的branch分支指向NULL

 

已知n个由小写字母构成的平均长度为10的单词,判断其中是否存在某个串为另一个串的前缀子串。下面对比3种方法:

1.    最容易想到的:即从字符串集中从头往后搜,看每个字符串是否为字符串集中某个字符串的前缀,复杂度为O(n^2)。

2.    使用hash:我们用hash存下所有字符串的所有的前缀子串。建立存有子串hash的复杂度为O(n*len)。查询的复杂度为O(n)* O(1)= O(n)。

3.    使用trie:因为当查询如字符串abc是否为某个字符串的前缀时,显然以b,c,d....等不是以a开头的字符串就不用查找了。所以建立trie的复杂度为O(n*len),而建立+查询在trie中是可以同时执行的,建立的过程也就可以成为查询的过程,hash就不能实现这个功能。所以总的复杂度为O(n*len),实际查询的复杂度只是O(len)。


解释一下hash为什么不能将建立与查询同时执行,例如有串:911,911456输入,如果要同时执行建立与查询,过程就是查询911,没有,然后存入9、91、911,查询911456,没有然后存入9114、91145、911456,而程序没有记忆功能,并不知道911在输入数据中出现过。所以用hash必须先存入所有子串,然后for循环查询。

而trie树便可以,存入911后,已经记录911为出现的字符串,在存入911456的过程中就能发现而输出答案;倒过来亦可以,先存入911456,在存入911时,当指针指向最后一个1时,程序会发现这个1已经存在,说明911必定是某个字符串的前缀,

trie树的简单实现:

#include <iostream>
 3using namespace std;
 4
 5const int branchNum = 26//声明常量 
 6int i;
 7
 8struct Trie_node
 9Trie树 - zhangzhibiao02005 - Technology HERE!{
10    bool isStr; //记录此处是否构成一个串。
11    Trie_node *next[branchNum];//指向各个子树的指针,下标0-25代表26字符
12    Trie_node():isStr(false)
13Trie树 - zhangzhibiao02005 - Technology HERE!    {
14        memset(next,NULL,sizeof(next));
15    }

16}
;
17
18class Trie
19Trie树 - zhangzhibiao02005 - Technology HERE!{
20public:
21    Trie();
22    void insert(const char* word);
23    bool search(char* word); 
24    void deleteTrie(Trie_node *root);
25private:
26    Trie_node* root;
27}
;
28
29Trie::Trie()
30Trie树 - zhangzhibiao02005 - Technology HERE!{
31    root = new Trie_node();
32}

33
34void Trie::insert(const char* word)
35Trie树 - zhangzhibiao02005 - Technology HERE!{
36    Trie_node *location = root;
37    while(*word)
38Trie树 - zhangzhibiao02005 - Technology HERE!    {
39        if(location->next[*word-'a'== NULL)//不存在则建立
40Trie树 - zhangzhibiao02005 - Technology HERE!        {
41            Trie_node *tmp = new Trie_node();
42            location->next[*word-'a'= tmp;
43        }
    
44        location = location->next[*word-'a']; //每插入一步,相当于有一个新串经过,指针要向下移动
45        word++;
46    }

47    location->isStr = true//到达尾部,标记一个串
48}

49
50bool Trie::search(char *word)
51Trie树 - zhangzhibiao02005 - Technology HERE!{
52    Trie_node *location = root;
53    while(*word && location)
54Trie树 - zhangzhibiao02005 - Technology HERE!    {
55        location = location->next[*word-'a'];
56        word++;
57    }

58    return(location!=NULL && location->isStr);
59}

60
61void Trie::deleteTrie(Trie_node *root)
62Trie树 - zhangzhibiao02005 - Technology HERE!{
63    for(i = 0< branchNum; i++)
64Trie树 - zhangzhibiao02005 - Technology HERE!    {
65        if(root->next[i] != NULL)
66Trie树 - zhangzhibiao02005 - Technology HERE!        {
67            deleteTrie(root->next[i]);
68        }

69    }

70    delete root;
71}

72
73void main() //简单测试
74Trie树 - zhangzhibiao02005 - Technology HERE!{
75    Trie t;
76    t.insert("a");         
77    t.insert("abandon");
78    char * = "abandoned";
79    t.insert(c);
80    t.insert("abashed");
81    if(t.search("abashed"))
82        printf("true\n");
83}

 

posted on 2012-07-05 23:05  沙场醉客  阅读(734)  评论(0编辑  收藏  举报