[Algorithm series 20] dictionary tree (Trie)

Source: Internet
Author: User

An overview

Also known as the word search tree, Trie tree, is a tree-shaped structure, is a hash tree variant. Typical applications are used for statistics, sorting, and saving a large number of strings (but not limited to strings), so it is often used by search engine systems for text frequency statistics.

Two advantages

Use the common prefix of the string to reduce query time, minimize meaningless string comparisons, and query efficiency is higher than hash tables.

Three properties

(1) The root node does not contain characters, and each node outside of the root node contains only one character;
(2) from the root node to a node, the path of the characters passed by the connection, the corresponding string for the node;
(3) All child nodes of each node contain different characters.

The word list is "apps", "Apply", "Apple", "append", "Back", "Backen" and "basic" the corresponding letter tree can be as shown.

For example, when you save Apple and apply, because their first four letters are the same, you want them to share the letters and store only the rest of the pieces separately. It is obvious that the alphabet tree makes good use of the common prefixes of the strings, saving storage space.

Four applications

(1) Fast retrieval of strings

Give a cooked word list of n words, and an article written in lowercase English, please write all the new words that are not in the cooked vocabulary in the first order of occurrence.
In this problem, we can use the array enumeration, with the hash, with the dictionary tree, the cooked word first to build a tree, and then read the article to compare, this method of efficiency is relatively high.

(2) Sort by "string"

Given n a different English name consisting of only one word, let you sort them in dictionary order from small to large output with a dictionary tree, using an array of ways to create a dictionary tree, and all the sons of each node of the tree are clearly sorted by their letter size. This tree is preceded by a sequential traversal.

(3) The longest common prefix

To create a dictionary tree for all strings, the length of the longest public prefix for the two strings is the number of public ancestors of the node they are in, and then the problem is transformed into the common ancestor problem.

Five implementations

The insertion (insert), delete, and find of a dictionary tree are very simple, with one heavy loop, that is, the first I loop finds the subtree corresponding to the first I letter, and then the corresponding operation. Implement this tree, we use the most common array to save, of course, we can open the dynamic pointer type. There are generally three ways to point a knot to a son:
(1) An array of alphabet size is opened for each node, and the corresponding subscript is the letter represented by the son, and the content is the position of the son corresponding to the large array, that is, the label;
(2) A linked list of each node, according to a certain sequence of records of each son who;
(3) Use left son right brother notation to record this tree.
Three methods, different. The first is easy to achieve, but the actual space requirements are large;
Easier to implement, space requirements are relatively small, but relatively time-consuming; third, space requirements are minimal, but relatively time-consuming and difficult to write. However, in general, a number of implementation methods are relatively simple, as long as the problem is a reasonable choice.

/* ---------------------------------------------* Date: 2015-02-21* sjf0115* title: 20. Dictionary tree * Source: Algorithm series * Blog:----------- ------------------------------------*/#include <iostream>#include <vector>#include <algorithm>using namespace STD;#define MAXstructtrienode{//The number of words ending with this node    intCount    Trienode *next[max]; Trienode (intx): Count (x) { for(inti =0; i < max;++i) {next[i] = NULL; }//for}};//InsertvoidInsert (trienode* &root,stringSTR) {intSize = Str.size ();intVal Trienode *p = root;//A one-character insert     for(inti =0; i < size;++i) {val = str[i]-' A ';//Does not have this character before        if(P->next[val] = = NULL) {P->next[val] =NewTrienode (0); }//ifp = p->next[val]; }//for    //At the end of the characterp->count++;}//DeletevoidDelete (trienode* &root,stringSTR) {intSize = Str.size ();intVal Trienode *p = root;//A one-character insert     for(inti =0; i < size;++i) {val = str[i]-' A ';//deleted string is not in dictionary        if(P->next[val] = = NULL) {return; }//ifp = p->next[val]; }//for    //At the end of the characterp->count--;}//FindBOOLSearch (trienode* root,stringSTR) {if(Root = NULL) {return false; }//if    intSize = Str.size (); Trienode *p = root;intVal for(inti =0; i < size;++i) {val = str[i]-' A ';//cannot transfer to next character        if(P->next[val] = = NULL) {return false; }//if        //Continue next characterp = p->next[val]; }//for    returnP->count >0;}//Print dictionaryvoidPrintdic (trienode* root, vector<vector<char> >&words, vector<char>&word) {if(Root = NULL) {return; }//if    if(Root->count >0) {words.push_back (word); }//if     for(inti =0; I < -; ++i) {if(Root->next[i]) {Word.push_back (' A '+i);            Printdic (Root->next[i],words,word);        Word.pop_back (); }//if}//for}intMain () {trienode* root =NewTrienode (0);//Insert    stringStrs[] = {"OK","Applition","App","Apple","Apply"}; for(inti =0; I <5; ++i) {Insert (root,strs[i]); }//for    stringStr"Apple");cout<<"Delete word ["<<str<<"] Previous query result:"<<endl;//Enquiry    if(Search (ROOT,STR)) {cout<<"word ["<<str<<"] in the dictionary"<<endl; }//if    Else{cout<<"word ["<<str<<"] Not in the dictionary"<<endl; }cout<<"Delete word ["<<str<<"]"<<endl;//DeleteDelete (ROOT,STR);cout<<"Delete word ["<<str<<"] After the query result:"<<endl;//Enquiry    if(Search (ROOT,STR)) {cout<<"word ["<<str<<"] in the dictionary"<<endl; }//if    Else{cout<<"word ["<<str<<"] Not in the dictionary"<<endl; }//Dictionary list    cout<<"Dictionary list:"<<endl; vector<vector<char> >Words vector<char>Word Printdic (Root,words,word); for(inti =0; i < words.size (); ++i) { for(intj =0; J < Words[i].size (); ++j) {cout<<words[i][j]; }//for        cout<<endl; }//for    return 0;}

Six references

Dictionary Tree Trie

An analysis of the application of Alphabet tree in the competition of informatics by algorithm collection

From Trie tree (dictionary tree) to suffix tree (10.28 revision)

If you have any questions, please correct me.

[Algorithm series 20] dictionary tree (Trie)

Related Article

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.