字典树的c++实现
来源:互联网 发布:php多态 编辑:程序博客网 时间:2024/05/16 17:12
//字典树//2016.6.6//yqtao@whu.edu.cn#include "stdafx.h"#include <iostream>#include <string>using namespace std;const int Num = 26;//struct TrieNode{ bool Isword;//判断是否是单词 TrieNode* next[Num]; TrieNode() :Isword(false)//初始化 { memset(next, NULL, sizeof(next)); }};class Trie{public: Trie() { root = new TrieNode(); } void insert(string word); bool search(string word); void deleteTrie(TrieNode* root);private: TrieNode* root;};void Trie::insert(string word){ TrieNode* location = root; for (int i = 0; i < word.length();i++) { if (location->next[word[i] - 'a'] == nullptr) { TrieNode* temp = new TrieNode(); location->next[word[i] - 'a']=temp; } location = location->next[word[i] - 'a']; } location->Isword = true;}bool Trie::search(string word){ TrieNode* location = root; //while (word&&location)//注意location不能为空 for (int i = 0; i < word.length()&&location;i++) location = location->next[word[i] - 'a']; return(location != NULL && location->Isword);}void Trie::deleteTrie(TrieNode* root){ for (int i = 0; i < Num; i++) { if (root->next[i] != NULL) { deleteTrie(root->next[i]); } } delete root;}void main() //简单测试 { Trie tree; int n;//输入n个单词在字典树中 cin >> n; while (n--) { string s; cin >> s; tree.insert(s); } string input; cout << "输入要检查的单词" << endl; cin >> input; cout << boolalpha << tree.search(input) << endl;//查找是否存在是个单词}
0 0
- 字典树C语言实现
- 字典树的实现
- 字典树的实现
- 字典树的实现
- 字典树(Trie树)的C程序实现代码
- 字典树的应用 单词意义查找-C语言实现
- 字典问题---c实现
- C 字典实现
- 字典树的动态实现
- 字典树 Trie 的实现
- 字典树的简单实现
- 字典树的C++实现
- 字典树的c++实现
- C 实现ArrayMap字典映射
- 字典(C#)之字典的工作原理
- Trie树(字典树)的实现
- Trie树(字典树)的实现
- HDU1671用指针实现的字典树
- php解析word文档
- 最大熵模型中的数学推导
- Java 学习之 多态性的理解
- Android开发笔记之ContentProvider 知识小结和总结
- OCP 052 001
- 字典树的c++实现
- CSS3翻页按钮
- 扩展欧几里得___追风少年的坐骑(2016swust信息院赛)
- Android生成桌面快捷图标
- Mon Jun 06 2016 08:00:00 GMT+0800 (中国标准时间)
- iOS RTMP 视频直播开发笔记(5)------- iOS H.264 实时硬编码实现原理
- PHP7 + iis7配置
- squirrel安装(连接hbase,整合phonenix)
- Xcode崩溃日志分析工具symbolicatecrash用法