Java实现Trie树

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++ ) {
            //用相对于a字母的值作为下标索引,也隐式地记录了该字母的值  
            int index=chrs[i]-'a';
            if (root.childs[index]!=null) {
                //已经存在了,该子节点prefix_num++
                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=root.childs[index];  
        }
    }
    /**
     * 遍历Trie树,查找所有的words以及出现次数 
     */
    public HashMap<String,Integer> getAllWords(){  
//      HashMap<String, Integer> map=new HashMap<String, Integer>();  

        return preTraversal(this.root, "");  
    }
    private HashMap<String, Integer> preTraversal(Node root, String prefixs) {
        // TODO Auto-generated method stub
        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 ");  
        }  
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值