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 _