字符串匹配算法之二------Trie字典树
2015-12-29 13:01
351 查看
Trie算法:
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; const int MAX = 26; class Trie{ private: struct TrieNode{ bool isTerminal; struct TrieNode **tbl; TrieNode():tbl(new struct TrieNode*[MAX]), isTerminal(false){ for (int i = 0; i != MAX; ++ i) tbl[i] = NULL; } }; typedef struct TrieNode *pTrieNode; pTrieNode root; public: Trie():root(new TrieNode){} ~Trie(){ deleteTrie(root); } void deleteTrie(pTrieNode trie); bool insert(string word); bool search(string word); }; /* ************************************************* 功能 : 删除以trie为父节点的trie树,包括每个节点里面的每个表 参数 : trie 返回值 : 无 ------------------------------------------------- 备注 : ------------------------------------------------- 作者 :Xu Weibin 时间 :2015-12-25 ************************************************* */ void Trie::deleteTrie(pTrieNode trie){ if (trie == NULL) return; for (int i = 0; i !=MAX; ++ i){ if (trie->tbl[i] != NULL){ deleteTrie(trie->tbl[i]); } } delete trie; } /* ************************************************* 功能 : 在以root为根节点的trie树中插入单词word 参数 : word,插入单词; 返回值 : true表示是否插入成功,false表示插入失败; ------------------------------------------------- 备注 : ------------------------------------------------- 作者 :Xu Weibin 时间 :2015-12-25 ************************************************* */ bool Trie::insert(string word){ int word_len = word.size(); int i = 0; pTrieNode current_node = root; int char_pos_at_tbl; while (i != word_len){ char_pos_at_tbl = word.at(i) - 'a'; //不存在该字母的节点,创建一个 if (current_node->tbl[char_pos_at_tbl] == NULL){ current_node->tbl[char_pos_at_tbl] = new TrieNode; if (!current_node->tbl[char_pos_at_tbl]) return false; //创建节点失败 } current_node = current_node->tbl[char_pos_at_tbl]; ++ i; } current_node->isTerminal = true; return true; } /* ************************************************* 功能 : 在以root为根节点的trie树中查找单词word 参数 : word,查找单词; 返回值 : true表示查找成功,false表示查找失败; ------------------------------------------------- 备注 : ------------------------------------------------- 作者 :Xu Weibin 时间 :2015-12-25 ************************************************* */ bool Trie::search(string word){ int word_len = word.size(); int i = 0; pTrieNode current_node = root; int char_pos_at_tbl; while(i != word_len){ char_pos_at_tbl = word.at(i) - 'a'; if (current_node->tbl[char_pos_at_tbl] == NULL){ return false; } else{ //判断是否到达字符串末尾 if (i == word_len - 1){ if (current_node->tbl[char_pos_at_tbl]->isTerminal){ return true; } else{ return false; } } current_node = current_node->tbl[char_pos_at_tbl]; ++ i; } } } int main(int argc, char *argv[]) { Trie trie; trie.insert("xu"); trie.insert("wei"); trie.insert("bin"); trie.insert("xuweibin"); cout << trie.search("xuweibing"); }
相关文章推荐
- office365为新建账号发送欢迎邮件
- 关于Ad Hoc发布的一点体会
- 图像处理和计算机视觉中的Gabor滤波:Gabor filter for image processing and computer vision
- Custom Container View Controller容器vc
- 2015-12-29 工作笔记-2
- Android退出应用最优雅的方式
- UVA 11361 - Investigating Div-Sum Property 数位DP
- Windows7添加SSD硬盘后重启卡住正在启动
- 例题5-8 UVA 400 Unix Is命令
- 杭电 最小生成树 2122 Ice_cream’s world III
- Android Notification.setLatestEventInfo()警告
- perl连接mysql
- Gabor滤波器:Gabor Filters
- loadrunner与ab对比
- 注册irc用户名以及申请irc频道 管理员退出后再登录如何获取管理员权限
- Linux C 数据结构——二叉树
- Get和Post区别,EncType提交数据的格式详解
- 算法导论22.4拓扑排序 练习总结
- (转)python requests的安装与简单运用
- [Phonegap+Sencha Touch] 移动开发72 List列表横向滑动操作(仿QQ列表滑动删除)