What is a trie tree?
◇ Trie tree is a multi-tree structure for quick search.
◇ Unlike the Binary Search Tree, each node in the trie tree does not store an element.
◇ The trie tree regards the keywords to be searched as a character sequence. The tree structure used for retrieval is constructed according to the order of keywords.
◇ Searching on the trie tree is similar to checking the English dictionary.
A m-degree trie tree can be empty or composed of M-degree trie trees.
For example, in an electronic English dictionary, you can create a trie tree to help you quickly retrieve English words. For example, a dictionary consists of the following words: a, B, C, AA, AB, AC, Ba, CA, ABA, ABC, Baa, Bab, Bac, cab, Abba, Baba, Caba, Abaca, caaba
Search on the trie treeFor example, search for words in the trie tree above.
ABA
(1) Search on the trie tree always starts at the root node.
(2) obtain the first letter (for example
A), And select the corresponding subtree according to the letter and go to the subtree to continue searching.
(3) obtain the second letter (for example
B), And then select the corresponding subtree for retrieval.
(4 )...
(5) If all the letters of a keyword have been removed from a node, the information attached to the node is read to complete the search.
About the trie tree:The time for searching a keyword in the trie tree is independent of the number of knots contained in the tree, and depends on the number of characters that constitute the keyword. The query time of the Binary Search Tree is related to the number of knots in the tree.
O(Log2
N). If the keyword to be searched can be broken down into character sequences and is not very long, the search speed using the trie tree is better than that of the Binary Search Tree. For example:
If the maximum length of a keyword is 5, you can use the trie tree to retrieve the specified keyword from the 265 = 11881376 possible keywords using 5 comparisons. Log2265 = 23.5 comparisons are performed at least on the binary search tree.
Trie class implementation and analysis:
# Include <iostream> # include <cstdio> # include <cstring> # include <cstdlib> # define maxn 10010 # define RST (n) memset (n, 0, sizeof (N) using namespace STD; const int char_num = 26; Class trie {public: trie (); int trie_search (const char * word, char * entry) const; int insert (const char * word, char * entry); int remove (const char * word, char * entry); protected: struct trie_node {char * data; trie_node * branch [Cha R_num]; // The cursor trie_node () ;}; trie_node * root ;}; trie: trie (): Root (null ){}; trie: trie_node () {DATA = NULL; For (INT I = 0; I <char_num; I ++) branch [I] = NULL;} int trie:: trie_search (const char * word, char * entry) const // search {int position = 0; char char_code; trie_node * location = root; while (location! = NULL & * word! = 0) {If (* word> = 'A' & * word <= 'Z') char_code = * word-'A '; else if (* word> = 'A' & * word <= 'Z') char_code = * word-'A'; else return 0; // invalid word location = Location-> branch [char_code]; position ++, word ++;} If (location! = NULL & location-> data! = NULL) {strcpy (entry, location-> data); return 1;} return 0;} int trie: insert (const char * word, char * entry) // insert {int result = 1, position = 0; If (root = NULL) root = new trie_node; char char_code; trie_node * location = root; while (location! = NULL & * word! = 0) {If (* word> = 'A' & * word <= 'Z') char_code = * word-'A '; else if (* word> = 'A' & * word <= 'Z') char_code = * word-'A'; else return 0; // invalid single word if (location-> branch [char_code] = NULL) location-> branch [char_code] = new trie_node; location = Location-> branch [char_code]; position ++, word ++;} If (location-> data! = NULL) Result = 0; else {location-> DATA = new char (strlen (entry) + 1); strcpy (location-> data, entry);} return result ;} int main () {trie t; char entry [100]; T. insert ("A", "det"); T. insert ("abacus", "noun"); T. insert ("abalone", "noun"); T. insert ("abandon", "verb"); T. insert ("abandoned", "adj"); T. insert ("Abashed", "adj"); T. insert ("Abate", "verb"); T. insert ("this", "pron"); If (T. trie_search ("this", entry)) Cout <"'This' was found. POs: "<entry <Endl; If (T. trie_search ("Abate", entry) cout <"'abate' is found. POs: "<entry <Endl; If (T. trie_search ("baby", entry) cout <"'baby' is found. POs: "<entry <Endl; else cout <" 'baby' does not exist at all! "<Endl; return 0 ;}
Dictionary tree simple knowledge and class implementation