Special statement:
The main post is the learning process of knowledge collation, in order to review later. Some of the content is from the network (if an excerpt is not indicated). If the content is wrong, please also correct me!
Series Articles:
1. Standard Trie dictionary Tree Learning One: Principle Analysis
2. Standard Trie Dictionary Tree Learning II: One of the ways to implement Java
Trie Tree is based on a simple Java implementation, on the code.
1. Defining the Node class Trienode
/*** Trienode Node class *@authorKonrad created on 2017/10/28*/ Public classTrienode {PrivateLinkedlist<trienode> children;//child Nodes Private CharData//node character Private intFreq//Frequency BooleanIsend;//whether it is a leaf node PublicTrienode (Chardata) { This. Children =NewLinkedlist<>(); This. Freq = 0; This. Isend =false; This. data =data; } PublicTrienode Childnode (Charc) { if(NULL!=children) { for(Trienode child:children) {if(Child.getdata () = =c) { returnChild ; } } } return NULL; } PublicLinkedlist<trienode>GetChildren () {returnchildren; } Public voidSetchildren (linkedlist<trienode>children) { This. Children =children; } Public CharGetData () {returndata; } Public voidSetData (Chardata) { This. data =data; } Public intGetfreq () {returnfreq; } Public voidSetfreq (intfreq) { This. Freq =freq; } Public voidAddfreq (intStep) { This. freq + =step; } Public voidSubfreq (intStep) { This. freq-=step; } Public BooleanIsend () {returnIsend; } Public voidSetEnd (Booleanend) {Isend=end; }}
2. Define Trie Tree Class Trietree
/*** Trietree trie Tree class * *@authorKonrad created on 2017/10/28*/ Public classTrietree {PrivateTrienode Root; PublicTrietree () { This. root =NewTrienode (' '); } //Find out if there is Public BooleanSearch (String word) {...} //Find returned nodes Publictrienode Searchnode (String Word) {...} //Insert Public voidInsert (String Word) {...} //removed from Public voidRemove (String word) {...} //Get Word frequency Public intgetfreq (String Word) {...}}
3. Inserting nodes
Public voidInsert (String Word) {Trienode current=Root; for(inti = 0; I < word.length (); i++) {Trienode child=Current.childnode (Word.charat (i)); if(NULL!=Child ) current=Child ; Else{Current.getchildren (). Add (NewTrienode (Word.charat (i))); Current=Current.childnode (Word.charat (i)); } current.addfreq (1); } current.setend (true); }
4. Finding nodes
//Find out if there is Public BooleanSearch (String word) {Trienode current=Root; for(inti = 0; I < word.length (); i++) { if(NULL==Current.childnode (Word.charat (i)))return false; Else Current=Current.childnode (Word.charat (i)); } if(Current.isend ())return true; Else return false; }//Find returned nodes Publictrienode Searchnode (String word) {Trienode current=Root; for(inti = 0; I < word.length (); i++) { if(NULL==Current.childnode (Word.charat (i)))return NULL; Else Current=Current.childnode (Word.charat (i)); } if(Current.isend ())returnCurrent ; Else return NULL; }
5. Delete a node
//removed from Public voidRemove (String word) {if(!Search (word))return; Trienode Current=Root; for(CharC:word.tochararray ()) {Trienode child=Current.childnode (c); if(Child.getfreq () = = 1) {Current.getchildren (). Remove (child); return; }Else{child.subfreq (1); Current=Child ; }} current.setend (false); }
6. How often to get a word
// get word frequency Public int getfreq (String word) { = Searchnode (word); if (null ! = Trienode) {return trienode.getfreq (); } Else { return 0; } }
7. Code Testing
/** * @authorKonrad created on 2017/10/28*/ Public classTrietreedemo { Public Static voidMain (string[] args) {String sentence= "Today is the Good day" and "What's your name at Facebook?" Doing here "; Trietree Trietree=NewTrietree (); for(String str:sentence.split ("") ) {Trietree.insert (str);//Insert node, build trie tree } //determine if there isSystem.out.println ("Does the word ' Facebook ' exists:" + trietree.search ("Facebook")); //statistical frequency of doSystem.out.println ("Frequent of the word ' you ':" + trietree.getfreq ("Do"))); //Remove TodaySystem.out.println ("Does the word ' Today ' exists-before Remove:" + trietree.search ("Today")); Trietree.remove ("Today"); System.out.println ("Does the word ' Today ' exists-after Remove:" + trietree.search ("Today")); }}
Output Result:
This is just one way to implement the trie tree, and it can be implemented in a different way.
Dictionary Tree Trie Learning II: One of the ways to implement Java