Trie Tree and Java implementation

Source: Internet
Author: User

From the English "Retrieval". Trie Tree is the word descriptor, the core idea is space change time.

Give a simple example.
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, the first place to appear.
This question can certainly use the hash, but I want to introduce is the trie tree. In some ways its use is greater. For example, for a word, I want to ask if its prefix has occurred. This kind of hash is not good, and the use of trie is still very simple.

Now back in the example, if we use the stupidest method, for each word, we're going to find out if there's a word in front of it. Then the complexity of the algorithm is O (n^2). Obviously, the range of 100000 is unacceptable. Now let's think about it in a different way. If the word I want to query is ABCD, then in the words in front of him, I obviously don't have to think about the beginning of b,c,d,f. It is possible to find out if there is ABCD in the beginning of a. Similarly, in the words beginning with a, we only consider B as the second letter ... The model of such a tree is becoming clearer ...

Let's say that there are 6 words b,abc,abd,bcd,abcd,efg,hii, the tree we built is like this.

For each node, the process of traversing from the root to his is a word, and if the node is marked red, that word exists, otherwise it does not exist.
So, for a word, I just follow him from the root to the corresponding node, and then see if the node is marked red to know if it has occurred. Mark this node red, which is equivalent to inserting the word.

We can see that the number of nodes in each layer of the trie tree is 26^i level. So in order to save space. We use dynamic lists, or we use arrays to simulate dynamics. The cost of space does not exceed the number of words x word length. (Turn from a Daniel)

the Java code of the Trie tree is implemented as follows: Import Java.util.arraylist;import java.util.iterator;import java.util.List;/** *//** * A word trie which can only deal with alphabeta letters. * @author Leeclipse * @since 2007-11-21*/ Public classtrie{PrivateVertex Root;//a trie tree has a root node//Inner class    protected classvertex{//Node Class        protected intwords; protected intprefixes; protectedVertex[] edges;//each node contains 26 child nodes (type itself)Vertex () {words=0; Prefixes=0; Edges=Newvertex[ -];  for(inti =0; i < edges.length; i++) {Edges[i]=NULL; }        }    }       PublicTrie () {root=NewVertex (); }       /** *//** * List all words in the Trie. * * @return*/     Publiclist< string>listallwords () {List< string> words =Newarraylist< string>(); Vertex[] Edges=root.edges;  for(inti =0; i < edges.length; i++) {            if(Edges[i]! =NULL) {String word=""+ (Char)('a'+i);            Depthfirstsearchwords (words, edges[i], word); }        }                returnwords; }     /** *//** * Depth first Search words in the Trie and add them to the List. * * @param words * @param vertex * @param wordsegment*/    Private voidDepthfirstsearchwords (List words, Vertex Vertex, String wordsegment) {vertex[] edges=vertex.edges; Boolean HasChildren=false;  for(inti =0; i < edges.length; i++) {            if(Edges[i]! =NULL) {HasChildren=true; String Newword= Wordsegment + (Char)('a'+i);            Depthfirstsearchwords (words, edges[i], Newword); }                    }        if(!HasChildren)        {Words.add (wordsegment); }    }     Public intcountprefixes (String prefix) {returncountprefixes (root, prefix); }    Private intcountprefixes (Vertex Vertex, String prefixsegment) {if(prefixsegment.length () = =0) {//reach The last character of the word            returnvertex.prefixes; }        Charc = Prefixsegment.charat (0); intindex = c-'a'; if(Vertex.edges[index] = =NULL) {//The Word does not exist            return 0; } Else {            returnCountprefixes (Vertex.edges[index], prefixsegment.substring (1)); }            }     Public intcountwords (String word) {returncountwords (root, Word); }        Private intcountwords (Vertex Vertex, String wordsegment) {if(wordsegment.length () = =0) {//reach The last character of the word            returnvertex.words; }        Charc = Wordsegment.charat (0); intindex = c-'a'; if(Vertex.edges[index] = =NULL) {//The Word does not exist            return 0; } Else {            returnCountWords (Vertex.edges[index], wordsegment.substring (1)); }            }        /** *//** * ADD a word to the Trie.     * * @param word the word to be added. */     Public voidAddword (String word) {Addword (root, Word); }        /** *//** * ADD the WORD from the specified vertex.     * @param vertex the specified vertex.     * @param word The word to be added. */    Private voidAddword (Vertex Vertex, String word) {if(word.length () = =0) {//If all characters of the word have been addedVertex.words + +; } Else{vertex.prefixes++; Charc = Word.charat (0); C=Character.tolowercase (c); intindex = c-'a'; if(Vertex.edges[index] = =NULL) {//If the edge does not existVertex.edges[index] =NewVertex (); } addword (Vertex.edges[index], word.substring (1));//go the next character        }    }     Public Static voidMain (String args[])//Just used for test{Trie Trie=NewTrie (); Trie.addword (" China"); Trie.addword (" China"); Trie.addword (" China"); Trie.addword ("Crawl"); Trie.addword ("Crime"); Trie.addword ("Ban"); Trie.addword (" China"); Trie.addword ("中文版"); Trie.addword ("Establish"); Trie.addword ("Eat"); System. out. println (trie.root.prefixes); System. out. println (Trie.root.words); List< string> list =trie.listallwords (); Iterator Listiterator=List.listiterator ();  while(Listiterator.hasnext ()) {String s=(String) listiterator.next (); System. out. println (s); }        intCount = Trie.countprefixes ("CH"); intCount1=trie.countwords (" China"); System. out. println ("The count of C prefixes:"+count); System. out. println ("The count of China CountWords:"+count1); }} run: C:\test>Java TrieTen0banchinacrawlcrimeeatenglishestablishthe Count of C prefixes:4The count of China CountWords:4

Trie Tree and Java implementation

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.