// //前缀树 TrieTree // import java.util.*; public class TrieTree{ private class Node{ //经过该字符的次数 private int pass; //以该字符结束的次数 private int end; //下一个字符 private HashMap route; public Node(int _pass,int _end){ pass = _pass; end = _end; route = new HashMap(); } } private Node head = null; public TrieTree(){ head = new Node(0,0); } public void add(String str){ if(str.isEmpty()) return; char[] charArr = str.toCharArray(); Node p = head; for (int i=0; i0){ char[] charArr = str.toCharArray(); Node p = head; Node n = null; for (int i=0; i map,TrieTree trie,String str){ HashMap result = new HashMap(); int count = 0; for (String _str : map.keySet()) { if(_str == str){ result.put(str,++count); } } //System.out.println("str = "+str+",mapCount = "+result.get(str)+",treeCount="+trie.searchStr(str)); return trie.searchStr(str) == result.get(str); } public static Boolean comparePref(HashMap map,TrieTree trie,String str){ HashMap result = new HashMap(); int count = 0; for (String _str : map.keySet()) { if(_str.startsWith(str)){ result.put(str,++count); } } return trie.searchPre(str) == result.get(str); } public static Boolean isTrue(){ int loopCount = 99999; for (int i=0; i map = new HashMap(); int random = (int)Math.random()*9999; if(random%2==0){ trie.add(str); map.put(str,1); } else{ trie.del(str); map.remove(str); } if(!compareEquals(map,trie,str)){ return false; } if(!comparePref(map,trie,str)){ return false; } } return true; } public static void main(String[] args){ System.out.println("result = "+isTrue()); } }