字典树(Trie树)的C程序实现代码
来源:互联网 发布:videoscribe 软件 编辑:程序博客网 时间:2024/04/29 23:36
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 256//ascii码有256个字符,故每棵树的子节点最多有256个
#define MAXLEN 256//单词最长为256
typedef struct TrieNode
{
int count;
struct TrieNode *next[MAX];
}TrieNode;
int i;
TrieNode *cur;
if(word[0]=='\0')
return;
cur=root;
for(i=0;word[i]!='\0';i++)
{
if(cur->next[word[i]]==NULL)
{
TrieNode *newNode = (TrieNode *)malloc(sizeof(TrieNode));
memset(newNode,0,sizeof(TrieNode));
cur->next[word[i]]=newNode;
}
cur=cur->next[word[i]];
}
cur->count++;
return;
}
{
char inStr[MAXLEN];
int size=0;
root = (TrieNode *)malloc(sizeof(TrieNode));
memset(root,0,sizeof(TrieNode));
while(1)
{
scanf("%s",inStr);
if(strcmp(inStr,"*")==0)
break;
Insert(inStr,root);
}
return;
}
{
static char theWord[MAXLEN];
static int pos=0;
int i;
if(curP==NULL)
return;
if(curP->count)
{
theWord[pos]='\0';
printf("%s:%d\n",theWord,curP->count);
}
for(i=0;i<MAX;i++)
{
pos--;
}
return;
}
{
int i;
TrieNode *cur;
cur=root;
for(i=0;word[i]!='\0';i++)
{
if(cur->next[word[i]]==NULL)
{
return false;
}
cur=cur->next[word[i]];
}
if(cur->count)
return true;
else
return false;
}
int main()
{
TrieNode *root;
char str[MAXLEN];
Construct(root);
printf("\n");
Traverse(root);
printf("\n");
while(1)
{
scanf("%s",str);
if(strcmp(str,"*")==0)
break;
printf("%s:%d\n",str,Find(root,str));
}
return 0;
}
//参考资料:http://blog.csdn.net/livelylittlefish/article/details/6410569
#include <string.h>
#include <stdlib.h>
#define MAX 256//ascii码有256个字符,故每棵树的子节点最多有256个
#define MAXLEN 256//单词最长为256
typedef struct TrieNode
{
int count;
struct TrieNode *next[MAX];
}TrieNode;
//插入一个单词
void Insert(char *word,TrieNode *root)
{int i;
TrieNode *cur;
if(word[0]=='\0')
return;
cur=root;
for(i=0;word[i]!='\0';i++)
{
if(cur->next[word[i]]==NULL)
{
TrieNode *newNode = (TrieNode *)malloc(sizeof(TrieNode));
memset(newNode,0,sizeof(TrieNode));
cur->next[word[i]]=newNode;
}
cur=cur->next[word[i]];
}
cur->count++;
return;
}
//创建树输入每个单词,以回车结束,则单词被插入树中,碰到*停止树的创建
{
char inStr[MAXLEN];
int size=0;
root = (TrieNode *)malloc(sizeof(TrieNode));
memset(root,0,sizeof(TrieNode));
while(1)
{
scanf("%s",inStr);
if(strcmp(inStr,"*")==0)
break;
Insert(inStr,root);
}
return;
}
//遍历整棵树
{
static char theWord[MAXLEN];
static int pos=0;
int i;
if(curP==NULL)
return;
if(curP->count)
{
theWord[pos]='\0';
printf("%s:%d\n",theWord,curP->count);
}
for(i=0;i<MAX;i++)
{
theWord[pos++]=i;
//从这句话可以看出在递归调用子节点前,子节点的值已经加入到单词中了
pos--;
}
return;
}
//查找一个单词是不是在树中
{
int i;
TrieNode *cur;
cur=root;
for(i=0;word[i]!='\0';i++)
{
if(cur->next[word[i]]==NULL)
{
return false;
}
cur=cur->next[word[i]];
}
if(cur->count)
return true;
else
return false;
}
int main()
{
TrieNode *root;
char str[MAXLEN];
Construct(root);
printf("\n");
Traverse(root);
printf("\n");
while(1)
{
scanf("%s",str);
if(strcmp(str,"*")==0)
break;
printf("%s:%d\n",str,Find(root,str));
}
return 0;
}
//参考资料:http://blog.csdn.net/livelylittlefish/article/details/6410569
- 字典树(Trie树)的C程序实现代码
- Trie树(字典树)的实现
- Trie树(字典树)的实现
- 字典树(Trie)的java实现
- Trie(字典树)的Java实现
- 字典树 Trie 的实现
- 前缀树(字典树)Trie的c代码
- trie字典树实现
- trie(字典树)的双数组实现
- 一个Trie字典树的简单实现
- 实现字典树Trie的基本操作
- 字典树(Trie)的简单实现
- 7. implement-trie(实现trie字典树)
- Trie树|字典树的简介及实现(转)
- Trie树|字典树的简介及实现(转)
- Trie树|字典树的简介及实现(转)
- Trie树(字典树)的原理及其实现
- Trie树(字典树)的C++实现
- 修改centos打开文件最大限制
- ASP.NET将Session保存到数据库中
- 从 2.4 到 2.6:Linux 内核可装载模块机制的改变对设备驱动的影响—linux2.4单独编译驱动模块
- asp.net基础代码
- JAVA连接Oracle数据库实代码+详细注释
- 字典树(Trie树)的C程序实现代码
- 第十三章 3.计时器
- 计算Tribonaci队列
- C#操作XML
- apache + tomcat + jk 负载均衡及虚拟主机实现
- Android程序颜色表
- mysql MyISAM InnoDB 区别
- linux下open函数和creat函数
- SQL Server2005用新建用户名登录提示“该用户与可信 SQL Server 连接无关联” 以及SQL Server2005常见错误及解决方案