Tire tree, also known as the Dictionary tree, is mainly used to find words, word frequency statistics.
The usual, directly on the code.
Package Tiretree;public class Tiretree {tirenode root;public tiretree (tirenode root) {this.root = root;} private void Insertelement (Tirenode root, String word) {if (Word = = NULL | | word.isempty ()) return;char[] elements = word.t Ochararray (); int index = elements[0]-' a '; if (Root.getnode () [index] = = null) {Root.getnode () [index] = new Tirenode (); root . GetNode () [Index].setelement (Elements[0]); if (word.length () = = 0) return;insertelement (root.getnode () [index], word.substring (1));} Private Boolean Searchword (Tirenode root, String word) {if (Word = = NULL | | word.isempty ()) return false; Tirenode p = Root;int index = 0;while (P! = NULL && index < Word.length ()) {int k = Word.charat (Index)-' a '; if (Root.getnode () [k]! = NULL) {if (index = = Word.length ()-1) {Root.getnode () [K].setfreq (Root.getnode () [K].getfreq () + 1); return true;} else {root = ro Ot.getnode () [k];index++;}} else {return false;}} return false;} public static void Main (string[] args) {Tirenode root = new Tirenode (); Tiretree TreE = new Tiretree (root); String context = "My name is Tom,what is your Name?" My name is Jenny. "; String target = "Tom"; for (string Word:TireTree.transContext (context)) {tree.insertelement (root, Word);} System.out.println (Tree.searchword (root, target));} public static string[] Transcontext (String context) {string c = Context.tolowercase (); c = C.replaceall ("[,?!.]", ""); c = C.substring (0, Context.length ()-1); return C.split ("");}} Class Tirenode {private tirenode[] node;private int freq;private char element;public void setElement (char element) {THIS.E Lement = element;} Public Tirenode () {node = new tirenode[26];freq = 0;} Public tirenode[] GetNode () {return node;} public int Getfreq () {return freq;} Public Char getelement () {return element;} public void Setnode (tirenode[] node) {this.node = node;} public void setfreq (int freq) {this.freq = Freq;}}
Java Tire Tree