Implement Trie (Prefix Tree)

Source: Internet
Author: User

Implement a trie with insert , search , and startsWith methods.

Trie, also known as the word search tree or the key tree, is a tree-shaped structure. Typical applications are used to count and sort large numbers of strings (but not just strings), so they are often used by search engine systems for text word frequency statistics or prefix matching.

It has 3 basic 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 characters that pass through the path are concatenated to the corresponding string for that node.
    3. All child nodes of each node contain different characters.
The following figure is the representation of Trie, where each edge represents a character, and if it ends, it is denoted by an asterisk. In this trie structure, we have the following strings, such as do, dork, dorm, etc., but there is no BA or Sen in Trie, because at the end of a, and N, there is no end sign (asterisk).

With such a data structure, we can use it to save a dictionary, to query whether the dictionary has the corresponding word, is it very convenient? We can also do smart hints, we put the user has searched for the word in the trie, whenever the user input a word, we can automatically prompt, such as when the user input BA, we will automatically prompt bat and Baii.

Transferred from: http://blog.csdn.net/beiyetengqing/article/details/7856113

The implementation code is as follows:

classTrienode {CharItem//the characters stored by the nodeLinkedList<TrieNode> ChildNodes;//all child node collections        BooleanIsend =false;//Word end tag//Initialize your data structure here.     PublicTrienode () {Item= ' '; ChildNodes=NewLinkedlist<trienode>(); }        //Querying child nodes     PublicTrienode Getchildnode (Charc) { for(Trienode childnode:childnodes) {if(CHILDNODE.ITEM==C)returnChildnode; }        return NULL; }        //Add child nodes     Public voidAddchildnode (Charc) {Trienode Childnode=NewTrienode (); Childnode.item=C;    Childnodes.add (Childnode); }} Public classTrie {PrivateTrienode Root;  PublicTrie () {root=NewTrienode (); }    //inserts a word into the trie.     Public voidInsert (String word) {if(Search (Word))return;//if the word already exists, return directlyTrienode cur=Root;  for(intI=0;i<word.length (); i++) {            if(Cur.getchildnode (Word.charat (i)) = =NULL) Cur.addchildnode (Word.charat (i));//Inserting child nodesCur =Cur.getchildnode (Word.charat (i)); } cur.isend=true;//Mark Word End    }    //Returns If the word is in the trie.     Public BooleanSearch (String word) {Trienode cur=Root;  for(intI=0;i<word.length (); i++) {            if(Cur.getchildnode (Word.charat (i)) = =NULL)return false; Cur=Cur.getchildnode (Word.charat (i)); }        returncur.isend==true; }    //Returns If there is any word in the trie//That's starts with the given prefix.     Public BooleanstartsWith (String prefix) {trienode cur=Root;  for(intI=0;i<prefix.length (); i++) {            if(Cur.getchildnode (Prefix.charat (i)) = =NULL)return false; Cur=Cur.getchildnode (Prefix.charat (i)); }        return true; }}//Your Trie object would be instantiated and called as such://Trie Trie = new Trie ();//Trie.insert ("somestring");//trie.search ("key");

Implement Trie (Prefix Tree)

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.