Java实现Trie树
代码块
import java.util.HashMap;
public class Trie_Tree {
private class Node {
private int dumpli_num;
private int prefix_num;
private Node childs[];
private boolean isLeaf;
public Node (){
dumpli_num=0 ;
prefix_num=0 ;
isLeaf=false ;
childs=new Node[26 ];
}
}
private Node root;
public Trie_Tree (){
root=new Node();
}
public void insert (String words){
insert(this .root,words);
}
private void insert (Node root,String words){
words=words.toLowerCase();
char [] chrs=words.toCharArray();
for (int i=0 ,length=chrs.length;i<length ;i++ ) {
int index=chrs[i]-'a' ;
if (root.childs[index]!=null ) {
root.childs[index].prefix_num++;
}else {
root.childs[index]=new Node();
root.childs[index].prefix_num++;
}
if (i==length-1 ) {
root.childs[index].isLeaf=true ;
root.childs[index].dumpli_num++;
}
root=root.childs[index];
}
}
/**
* 遍历Trie树,查找所有的words以及出现次数
*/
public HashMap<String,Integer> getAllWords (){
return preTraversal(this .root, "" );
}
private HashMap<String, Integer> preTraversal (Node root, String prefixs) {
HashMap<String, Integer> map=new HashMap<String, Integer>();
if (root!=null ){
if (root.isLeaf==true ){
map.put(prefixs, root.dumpli_num);
}
for (int i=0 ,length=root.childs.length;i<length;i++){
if (root.childs[i]!=null ){
char ch=(char )(i+'a' );
String tempStr=prefixs+ch;
map.putAll(preTraversal(root.childs[i], tempStr));
}
}
}
return map;
}
/**
*判断某字串是否在字典树中
*/
public boolean isExist (String word){
return search(this .root, word);
}
private boolean search (Node root,String word){
char [] chs=word.toLowerCase().toCharArray();
for (int i=0 ,length=chs.length; i<length;i++){
int index=chs[i]-'a' ;
if (root.childs[index]==null ){
return false ;
}
root=root.childs[index];
}
return true ;
}
/**
* 得到以某字串为前缀的字串集,包括字串本身! 类似单词输入法的联想功能
* @param prefix 字串前缀
* @return 字串集以及出现次数,如果不存在则返回null
*/
public HashMap<String, Integer> getWordsForPrefix (String prefix){
return getWordsForPrefix(this .root, prefix);
}
/**
* 得到以某字串为前缀的字串集,包括字串本身!
* @param root
* @param prefix
* @return 字串集以及出现次数
*/
private HashMap<String, Integer> getWordsForPrefix (Node root,String prefix){
HashMap<String, Integer> map=new HashMap<String, Integer>();
char [] chrs=prefix.toLowerCase().toCharArray();
for (int i=0 , length=chrs.length; i<length; i++){
int index=chrs[i]-'a' ;
if (root.childs[index]==null ){
return null ;
}
root=root.childs[index];
}
return preTraversal(root, prefix);
}
}
import java.util .HashMap
public class Trie_Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Trie_Tree trie = new Trie_Tree()
trie.insert ("I" )
trie.insert ("Love" )
trie.insert ("China" )
trie.insert ("China" )
trie.insert ("China" )
trie.insert ("China" )
trie.insert ("China" )
trie.insert ("xiaoliang" )
trie.insert ("xiaoliang" )
trie.insert ("man" )
trie.insert ("handsome" )
trie.insert ("love" )
trie.insert ("chinaha" )
trie.insert ("her" )
trie.insert ("know" )
HashMap<String,Integer> map=trie.getAllWords ()
for(String key:map.keySet ()){
System.out .println (key+" 出现: " + map.get (key)+"次" )
}
map=trie.getWordsForPrefix ("chin" )
System.out .println ("\n\n包含chin(包括本身)前缀的单词及出现次数:" )
for(String key:map.keySet ()){
System.out .println (key+" 出现: " + map.get (key)+"次" )
}
if(trie.isExist ("xiaoming" )==false){
System.out .println ("\n\n字典树中不存在:xiaoming " )
}
}
}