Tried树 (字典查找树)的创建
2016-07-14 22:20
218 查看
数的创建
//字典查找树的实现
#include<iostream>
using namespace std;
struct Tried
{
int count;//用来标记该节点是个可以形成一个单词,如果count!=0,则从根节点到该节点的路径可以形成一个单词
Tried* next[26];
};
Tried* createNode()
{
Tried* node = new Tried();
node->count = 0;
memset(node->next,0,sizeof(node->next));
return node;
}
//向树中插入单词
void insert(Tried* root,char* word)
{
if (root == NULL || *word == '\0')return;
int len = strlen(word);
Tried* node = root;
char* p = word;
while(*p!='\0')
{
if (node->next[*p - 'a'] == NULL)
{
node->next[*p - 'a'] = createNode();
}
node = node->next[*p-'a'];
p++;
}
node->count++;
}
//查找单词
bool search(Tried * root,char* word)
{
if (root==NULL||*word=='\0')return false;
Tried* node = root;
char* p = word;
while (*p!='\0')
{
if (node->next[*p - 'a'] == NULL)
break;
node = node->next[*p-'a'];
p++;
}
if (*p != '/0')return false;
else
{
if (node->count != 0)return true;
else return false;
}
}
//返回单词查询次数
int count(Tried* root,char* word)
{
if (root == NULL || *word == '\0')return 0;
char* p = word;
Tried* node = root;
while (*p!='\0')
{
node = node->next[*p-'a'];
p++;
}
return node->count;
}
主函数
int main()
{
Tried* root = createNode();
char word[][10] = {"test", "study", "open", "show", "shit", "work", "work", "test", "tea", "word", "area", "word", "test", "test", "test"};
//用word创建树
for (int i = 0; i < 15;i++)
{
insert(root, word[i]);
}
for (int i = 0; i < 15; i++){
cout << "the word " << word[i]<< " appears" << count(root, word[i]) << " times in the trie-tree" << endl;
}
//查找树中没有的单词
char s[10] = "testit";
if (!search(root, s))
cout << " the word testit isn't appear in the tree" << endl;
return 0;
}
//字典查找树的实现
#include<iostream>
using namespace std;
struct Tried
{
int count;//用来标记该节点是个可以形成一个单词,如果count!=0,则从根节点到该节点的路径可以形成一个单词
Tried* next[26];
};
Tried* createNode()
{
Tried* node = new Tried();
node->count = 0;
memset(node->next,0,sizeof(node->next));
return node;
}
//向树中插入单词
void insert(Tried* root,char* word)
{
if (root == NULL || *word == '\0')return;
int len = strlen(word);
Tried* node = root;
char* p = word;
while(*p!='\0')
{
if (node->next[*p - 'a'] == NULL)
{
node->next[*p - 'a'] = createNode();
}
node = node->next[*p-'a'];
p++;
}
node->count++;
}
//查找单词
bool search(Tried * root,char* word)
{
if (root==NULL||*word=='\0')return false;
Tried* node = root;
char* p = word;
while (*p!='\0')
{
if (node->next[*p - 'a'] == NULL)
break;
node = node->next[*p-'a'];
p++;
}
if (*p != '/0')return false;
else
{
if (node->count != 0)return true;
else return false;
}
}
//返回单词查询次数
int count(Tried* root,char* word)
{
if (root == NULL || *word == '\0')return 0;
char* p = word;
Tried* node = root;
while (*p!='\0')
{
node = node->next[*p-'a'];
p++;
}
return node->count;
}
主函数
int main()
{
Tried* root = createNode();
char word[][10] = {"test", "study", "open", "show", "shit", "work", "work", "test", "tea", "word", "area", "word", "test", "test", "test"};
//用word创建树
for (int i = 0; i < 15;i++)
{
insert(root, word[i]);
}
for (int i = 0; i < 15; i++){
cout << "the word " << word[i]<< " appears" << count(root, word[i]) << " times in the trie-tree" << endl;
}
//查找树中没有的单词
char s[10] = "testit";
if (!search(root, s))
cout << " the word testit isn't appear in the tree" << endl;
return 0;
}
相关文章推荐
- Unity3D 2D入门——第2天:C#,我跟C++有点眼缘
- Spring杂记
- POCO库——Foundation组件之日期时间DateTime
- spring整合hibernate
- UVA 107 The Cat in the Hat
- Windows查看CPU的内核个数
- HDU 4258 Covered Walkway 斜率优化DP
- 三国的演义
- 畅通工程(并查集)
- Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'armv7'
- C语言实现 迪杰斯特拉算法
- xgboost/gbdt在调参时为什么树的深度很少就能达到很高的精度?
- 劣质的草 (连通分量)
- C语言培训_009
- Oracle常用的一些命令
- Swift基本语法(3)-控制流
- Jade学习篇
- junit命令行和testNg命令行
- 44. Wildcard Matching
- 修改表常用语法