一个Trie树的实现
来源:互联网 发布:linux系统巡检脚本 编辑:程序博客网 时间:2024/06/06 00:26
Trie树实现代码:
// Max branch number. Such as 26 for lower case letter set, 10 for digit character set.const int MAX_BRANCH_NUM = 26;// Node of Trie Tree class CTrieNode{public:CTrieNode(){nPrefixCount = 0;nWordCount = 0;memset(next, NULL, sizeof(next));}public:int nPrefixCount; // Count of prefix in the Trie Treeint nWordCount; // Count of word in the Trie TreeCTrieNode* next[MAX_BRANCH_NUM]; // Point to all the nodes in the next level};// Trie Tree classclass CTrieTree{public:CTrieTree(); // Constructor~CTrieTree(); // Destructorint GetIndex(char ch) const; // Get the index of 'ch' in the 'next' arraybool CheckWord(const char *s) const; // Check if there is illegal characterbool InsertWord(const char *s) const; // Insert a wordint SearchPrefix(const char *s) const; // Search the count of a prefixint SearchWord(const char *s) const; // Search the count of a wordbool DeleteWord(const char *s) const; // Delete a wordprivate:void FreeTreeNodes(CTrieNode * root); // Free those tree nodesprivate:CTrieNode *pRoot; // Root node pointer of Trie Tree};// ConstructorCTrieTree::CTrieTree(){pRoot = new CTrieNode; }// DestructorCTrieTree::~CTrieTree(){FreeTreeNodes(pRoot);}// Get the index of 'ch' in the 'next' arrayint CTrieTree::GetIndex(char ch) const{// This is for lower case letter setreturn ch - 'a';}// Check if there is illegal character in the wordbool CTrieTree::CheckWord(const char *s) const{// This is for lower case letter setfor(int i = 0; s[i] != '\0'; i++)if(s[i] < 'a' || s[i] > 'z')return false;return true;}// Insert a wordbool CTrieTree::InsertWord(const char *s) const{// Can't insert a null string or an word containing illegal characterif((s[0] == '\0') || (CheckWord(s)== false))return false;CTrieNode * pCurrent = pRoot;int i = 0;while(1){int nIndex = GetIndex(s[i]);if(pCurrent->next[nIndex] == NULL)pCurrent->next[nIndex] = new CTrieNode;pCurrent = pCurrent->next[nIndex];pCurrent->nPrefixCount++;i++;if(s[i] == '\0'){pCurrent->nWordCount++;break;}}return true;};// Search the count of a prefix in the Trie Treeint CTrieTree::SearchPrefix(const char *s) const{// There are no null prefix or a prefix containing illegal characterif((s[0] == '\0') || (CheckWord(s)== false))return 0;CTrieNode * pCurrent = pRoot;int i = 0;while(1){int nIndex = GetIndex(s[i]);pCurrent = pCurrent->next[nIndex];if((pCurrent == NULL) || (pCurrent->nPrefixCount == 0))return 0;i++;if(s[i] == '\0')return pCurrent->nPrefixCount;}}// Search the count of a word in the Trie Treeint CTrieTree::SearchWord(const char *s) const{// There are no null word or a word containing illegal characterif((s[0] == '\0') || (CheckWord(s)== false))return 0;CTrieNode * pCurrent = pRoot;int i = 0;while(1){int nIndex = GetIndex(s[i]);pCurrent = pCurrent->next[nIndex];if((pCurrent == NULL) || (pCurrent->nPrefixCount == 0))return 0;i++;if(s[i] == '\0')return pCurrent->nWordCount;}}// Delete a wordbool CTrieTree::DeleteWord(const char *s) const{if(!SearchWord(s))return false;CTrieNode * pCurrent = pRoot;int i = 0;while(1){int nIndex = GetIndex(s[i]);pCurrent = pCurrent->next[nIndex];pCurrent->nPrefixCount--;i++;if(s[i] == '\0'){pCurrent->nWordCount--;break;}}return true;}// Free those tree nodesvoid CTrieTree::FreeTreeNodes(CTrieNode * pRoot){if(pRoot == NULL)return;for(int i = 0; i < MAX_BRANCH_NUM; i++)FreeTreeNodes(pRoot->next[i]);free(pRoot);pRoot = NULL;}
- 一个Trie树的实现
- Trie的一个实现
- 一个Trie字典树的简单实现
- 一个简单的Trie树实现
- 一个Trie树的简单实现
- Trie树的实现
- Trie树的实现
- 一个通用的Trie树,标准C++实现
- 一个通用的Trie树,标准C++实现
- 一个通用的Trie树,标准C++实现
- lintcode 实现一个 Trie
- Trie树数据结构的实现
- Trie树的C++实现
- Trie 树的简单实现
- Trie树的C++实现
- 字典树 Trie 的实现
- Trie树的C++实现
- Trie树的C++实现
- setmappath与三层结构
- SQL CASE WHEN使用
- .net正则表达式逆序环视中反向引用来看匹配过程。
- Content Provider应用实例
- Google Guice入门教程2
- 一个Trie树的实现
- Activity和Task的基本模型
- 看看老师给的存储过程把
- G-Wan Application Web Server
- 回忆钱晓威君
- JDO-1
- 【二分图+最小路径覆盖+建图难度】北大 poj 1548 Robots
- 李宁项目收获四(环境变量配置)
- 凡客五轮融资史 ,3年融资总额达2亿美元