Implementation of a trie tree

Source: Internet
Author: User

Trie tree implementation code:

// 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.