Dictionary tree template (java)

Source: Internet
Author: User

Dictionary tree template (java)

Class Trie {private int SIZE = 26; private TrieNode root; // dictionary tree root Trie () {// initialize dictionary tree root = new TrieNode ();} private class TrieNode {// dictionary Tree node private int num; // Number of words passed through this node, that is, the number of times that node characters appear private TrieNode [] son; // all the son nodes are private boolean isEnd; // whether the last node is private char val; // The value of the node is TrieNode () {num = 1; son = new TrieNode [SIZE]; isEnd = false ;}// create a dictionary tree public void insert (String str) {// insert a word if (str = null | str. length () = 0) {return;} TrieNode node = root; char [] letters = str. toCharArray (); for (int I = 0, len = str. length (); I <len; I ++) {int pos = letters [I]-'A'; if (node. son [pos] = null) {node. son [pos] = new TrieNode (); node. son [pos]. val = letters [I];} else {node. son [pos]. num ++;} node = node. son [pos];} node. isEnd = true;} // calculates the number of word prefixes. public int countPrefix (String prefix) {if (prefix = null | prefix. length () = 0) {return-1;} TrieNode node = root; char [] letters = prefix. toCharArray (); for (int I = 0, len = prefix. length (); I
 
  
See encyclopedia specific: http://baike.baidu.com/link? Url = bImDvvYau9FWbZKr64ExkxvXOZb_Df-b7O2YCPHyqH_orknkoOi6JT4O-3a9XqorwbugAnTibEq0pFjx-Gvu8 _
 

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.