Trie trie =newTrie();//创建一个树根for(String word : words){//将一个保存单词的数组words插入到树
trie.insert(word);}
classTrie{String word;Map<Character,Trie> children;boolean isWord;publicTrie(){this.word ="";this.children =newHashMap<Character,Trie>();}publicvoidinsert(String word){//使用该方法将一个单纯插入到树中Trie cur =this;for(int i =0; i < word.length();++i){char c = word.charAt(i);if(!cur.children.containsKey(c)){
cur.children.put(c,newTrie());}
cur = cur.children.get(c);}
cur.word = word;}}