Java Tire Tree

Source: Internet
Author: User

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

Related Article

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.