Trie tree, or dictionary tree, is a tree structure that minimizes meaningless string comparisons; typical applications are used to count and sort large numbers of strings (but not just strings);
Basic knowledge
(1) The core idea of trie is to change the time of space and use the common prefix of string to reduce the cost of query time to achieve the goal of improving efficiency.
(2) The root node does not contain characters, each node outside the root node contains only one character , and the node holds the corresponding character value;
(3) from the root node to a node, the path through the characters (nodes) connected together, the corresponding string for the node;
(4) All child nodes of each node contain different characters;
(5) Because some words are prefixes of other words, each node increment is the identifier of the word ;
Basic implementation
struct trienode{char value; Vector<trienode*> children; bool Word;}; Class Trie{public:trie () {root = new Trienode (); Root->word = false; } ~trie () {_deletetrienode (root); } void Insert (string s) {trienode* node = root; for (int i=0; i < s.size (); ++i) {if (!isalpha (S[i])) return; vector<trienode*>& children = node->children; if (Children.empty ()) children.assign (+, NULL); int index = ToLower (s[i])-' a '; if (Children[index]) {node = Children[index]; if (i = = S.size ()-1) Node->word = true; } else {trienode* child = new Trienode (); Child->value = S[i]; if (I < s.size ()-1) {Child->word = false; } else {Child->word = true; } Children[index] = child; node = child; }}} bool Search (string key) {trienode* node = root; int i = 0; BOOL flag = FALSE; for (i=0; i < key.size (); ++i) {if (!isalpha (Key[i])) return false; vector<trienode*>& children = node->children; if (Children.empty ()) return false; int index = ToLower (Key[i])-' a '; trienode* child = Children[index]; if (child) {node = child; Flag = child->word; } else {return false; }} return flag; } bool StartsWith (string prefix) {trienode* node = root; for (int i=0; i < prefix.size (); ++i) {if (!isalpha (Prefix[i])) return false; vector<trienode*>& children = node->children; if (Children.empty ()) return false; int index = ToLower (Prefix[i])-' a '; trienode* child = Children[index]; if (chilD) {node = child; } else {return false; }} return true; }private:void _deletetrienode (trienode* node) {for (int i=0; node && i < Node->children.size (); ++i) _deletetrienode (Node->children[i]); if (node) Delete node; } trienode* root;};
Explain the points
(1) The establishment of the Trie tree of this program, only the corresponding lowercase characters of the dictionary tree, for uppercase characters will be converted to lowercase characters, encountered non-word character will be returned in advance;
(2) in Trienode, Word identifies whether the node is a single word;
(3) in the destructor, the memory of each node is freed by recursive return;
Basic applications
Description: Give you 100,000 words with a length of not more than 10. For each word, we have to judge if he has appeared, and if so, for the first time appear in the number of positions.
Workaround:
(1) Since the word length does not exceed 10, the establishment of the trie tree is also tolerable, the cost of space will not exceed (the number of words * word length) of a trienode, but also in the edge of the query to establish the trie tree;
(2) If the word first appears, the query establishes the trie tree, in the last node identifies the first occurrence of the word position, you can modify the Trienode word identity type int (the default is 1), storing the first occurrence of the position;
(3) If the word does not appear for the first time, when the last character of the word is queried, the word ID of the character is not-1, indicating that the word has already appeared, and the value of Word is the first occurrence of the word;
"Data Structure" trie tree