字典树 找出单词文件中出现次数前十的单词

import java.io.*;
import java.util.*;

/**
 * 不区分大小写
 */
class TrieNode{
    TrieNode[] next = new TrieNode[26]; // 只有小写字母的字典树    0存放a   1存放b ...   25存放z
    int count = 0;//字母出现的次数
    int wordCount = 0;// 单词出现的次数
    char currentChar;
    public TrieNode(char word) {
        this.currentChar = word;
    }
}

public class Main {

    public static void main(String[] args) throws IOException{
        //读取文件(字符流)
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\word.txt"),"GBK"));// 这里主要是涉及中文
        //读取数据
        //循环取出数据
        String str = null;
        TrieNode root = new TrieNode('0'); // 初始化根节点
        while ((str = in.readLine()) != null) {
            insert(root,str);
        }


        // 查找单词出现的次数
       // int count = query(root, "world");
       // System.out.println(count);
        // 查找单词出现的次数最多的10个
        Map map = new TreeMap();// 默认按照key 升序排序
        in = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\word.txt"),"GBK"));
        while ((str = in.readLine()) != null) {
            int query = query(root, str);
            map.put(str,query);
        }
        // 按值排序  降序排序
        List> list = new ArrayList>(map.entrySet());
        Collections.sort(list, new Comparator>() {
            @Override
            public int compare(Map.Entry o1, Map.Entry o2) {
                return -o1.getValue().compareTo(o2.getValue());
            }
        });
        int i=0;
        for (Map.Entry mapping : list) {
            if(i++ == 10) {
                break;
            }
            System.out.println("单词:" + mapping.getKey() + " 出现:" + mapping.getValue());
        }

        //关闭流
        in.close();


    }

    public static  void insert(TrieNode root,String str) {
        str = str.toLowerCase();
        char[] chars = str.toCharArray();
        for(int i=0;i 
 

  

你可能感兴趣的:(字典树 找出单词文件中出现次数前十的单词)