【数据结构-trie树】trie数实现单词查询和单词统计
2012-09-07 10:48
411 查看
参考内容:
1. 这位童鞋的文章 http://blog.csdn.net/zhulei632/article/details/6704496
2. 严蔚敏 -数据结构
如图所示,一个Trie树的结构如下:
![](http://img.my.csdn.net/uploads/201209/07/1346985134_2226.png)
如果在trie树的node节点添加新的域 count,记录已有的单词总数。那么,Trie树除了实现单词查询之外,还可以实现单词频度统计。
如,我们定义的Trie树的节点结构如下:
有了这个结构。Trie树的实现就有了一个基础。
根据该算法,代码不难写出:
![](http://img.my.csdn.net/uploads/201209/07/1346985807_6795.png)
实现检索的代码:
完整的测试代码如下:
运行结果如下:
1. 这位童鞋的文章 http://blog.csdn.net/zhulei632/article/details/6704496
2. 严蔚敏 -数据结构
1.键树的定义:
键树又叫“数字查找树”。深度>=2 . 树中的每个节点一般不是直接包含关键字,而是包含组成关键字的符号(当然叶子节点除外,叶子节点可能包含整个单词以及词频,非叶节点也可包含单词和词频)。根据存储结构的不同,又分为双链树和多重链表树。或者就是常说的“Trie树”,取自检索“retrieve”中间的四个单词。因此也被称为检索树。Trie树的每个节点含有d个指针域(d为关键字的基数,如果是字母,那么基数为26 即a-z .如果是数字,那么基数是10, 即0-9)。如图所示,一个Trie树的结构如下:
![](http://img.my.csdn.net/uploads/201209/07/1346985134_2226.png)
如果在trie树的node节点添加新的域 count,记录已有的单词总数。那么,Trie树除了实现单词查询之外,还可以实现单词频度统计。
如,我们定义的Trie树的节点结构如下:
typedef struct Trie_node{ int count; struct Trie_node *next[26]; }TrieNode, *Trie;其中next数组指向下一层次节点。
有了这个结构。Trie树的实现就有了一个基础。
2。Trie树的建立
建立一个Trie树的过程就是不断添加新的单词的过程。由根节点向下扫描,如果不存在相应的节点则创建之。否则进入下一个层次,直到单词添加完毕。根据该算法,代码不难写出:
//创建新的节点 TrieNode* createTrieNode(){ TrieNode* root = (TrieNode*)malloc(sizeof(TrieNode)); root->count = 0; memset(root->next, 0, sizeof(root->next)); return root; } //插入单词。 void trie_insert(Trie root, char* word){ TrieNode* node = root; char *p = word; while(*p) { if(NULL == node->next[*p-'a']) { node->next[*p-'a'] = createTrieNode(); } node = node->next[*p-'a']; p++; } node->count += 1; }
3 .Trie树的检索
Trie树中检索的过程是走一条从跟节点开始到叶子节点的路径(不一定走到叶子节点,取决于你的Trie树的实现,如果规定每个单词以$结束,那么检索成功的话一定走到叶子节点):如下图示意,检索单词bat的路径用红色标出。![](http://img.my.csdn.net/uploads/201209/07/1346985807_6795.png)
实现检索的代码:
int trie_search(Trie root, char* word){ TrieNode* node = root; char *p = word; while(*p && node!=NULL) { node = node->next[*p-'a']; p++; } return (node != NULL && node->count > 0); }
4. 利用Trie树实现词频统计
如上文所述:在Trie的node节点中添加count域后,可以统计单词出现的次数。统计的方法就是在插入单词的时候,令相应的count域加1(初始化为0)。代码见Trie插入部分。完整的测试代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
typedef struct Trie_node{ int count; struct Trie_node *next[26]; }TrieNode, *Trie;
TrieNode* createTrieNode(){
TrieNode* root = (TrieNode*)malloc(sizeof(TrieNode));
root->count = 0;
memset(root->next, 0, sizeof(root->next));
return root;
}
void trie_insert(Trie root, char* word){
TrieNode* node = root;
char *p = word;
while(*p){
if(NULL == node->next[*p-'a']){
node->next[*p-'a'] = createTrieNode();
}
node = node->next[*p-'a'];
p++;
}
node->count += 1;
}
int trie_search(Trie root, char* word){
TrieNode* node = root;
char *p = word;
while(*p && node!=NULL){
node = node->next[*p-'a'];
p++;
}
return (node != NULL && node->count > 0);
}
int trie_word_count(Trie root, char* word){
TrieNode * node = root;
char *p = word;
while(*p &&node != NULL){
node = node->next[*p-'a'];
p++;
}
return node->count;
}
int main(){
Trie t = createTrieNode();
char word[][10] = {"test","study","open","show","shit","work","work","test","tea","word","area","word","test","test","test"};
for(int i = 0;i < 15;i++ ){
trie_insert(t,word[i]);
}
for(int i = 0;i < 15;i++ ){
printf("the word %s appears %d times in the trie-tree\n",word[i],trie_word_count(t,word[i]));
}
char s[10] = "testit";
printf("the word %s exist? %d \n",s,trie_search(t,s));
return 0;
}
运行结果如下:
![](http://img.my.csdn.net/uploads/201209/07/1346986360_8158.jpg)
相关文章推荐
- 【数据结构-trie树】trie数实现单词查询和单词统计
- 用Trie树实现词频统计和单词查询
- java实现读取一篇英文文章,统计其中每个单词出现的次数并排序输出
- 通过JDBC实现对Oracle,mysql数据库的增删改查,模糊查询,查询全部,根据ID查询,分页查询,统计查询,
- 数据结构-4-Trie树:应用于统计、排序与搜索 原理详解
- java实现的Trie树数据结构
- 查询分类并实现分类统计SQL存储过程
- 对Trie单词树的实现与思考
- 如何用Python实现任一个英文的纯文本文件,统计其中的单词出现的个数?
- java实现文件单词频率统计
- 统计查询-根据条件进行count的两种实现方式- oracle
- HttpGet方式调用有道词典API实现单词查询功能
- Hadoop之——以1.x版本和0.x版本分别实现单词统计功能
- 搜索二叉树应用---判断单词正确与否,简单字典的实现,出现ip次数的统计
- 使用trie树统计单词出现的频数
- 一条sql语句实现统计查询
- Mongoose如何实现统计查询、关联查询
- C语言实现文件单词统计
- Python实现统计英文单词个数及字符串分割代码
- Trie树的创建、插入、查询的实现