/*** This reference program is provided by @jiuzhang.com* Copyright is reserved. Please indicate the source for forwarding*//** * Definition of TrieNode: * public class TrieNode { * public NavigableMap<Character, TrieNode> children; * public List<Integer> top10; * public TrieNode() { * children = new TreeMap<Character, TrieNode>(); * List<Integer> top10 = new ArrayList<Integer>(); * } * } */publicclassTrieService {privateTrieNode root =null;publicTrieService() { root =newTrieNode(); }publicTrieNodegetRoot() {// Return root of trie root, and // lintcode will print the tree struct.return root; }// @param word a string// @param frequency an integerpublicvoidinsert(String word,int frequency) {// Write your cod hereTrieNode cur = root;int n =word.length();for (int i =0; i < n; ++i) {Character c =word.charAt(i);if (!cur.children.containsKey(c))cur.children.put(c,newTrieNode()); cur =cur.children.get(c);addFrequency(cur.top10, frequency); } }publicvoidaddFrequency(List<Integer> top10,int frequency) {top10.add(frequency);int n =top10.size();int index = n -1;while (index >0) {if (top10.get(index) >top10.get(index -1)) {int temp1 =top10.get(index);int temp2 =top10.get(index -1);top10.set(index, temp2);top10.set(index -1, temp1); index -=1; } elsebreak; }if (n >10)top10.remove(n -1); } }