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:
- The root node does not contain characters, and each node outside of the root node contains only one character.
- From the root node to a node, the characters that pass through the path are concatenated to the corresponding string for that node.
- 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)