基本实现 存储 查找
import com.sun.org.apache.xpath.internal.operations.Bool;
import java.util.TreeMap;
public class MyTrie {
private class Node{
public boolean isEnd;
public TreeMap<Character,Node> next;
public Node(){
this.isEnd = false;
}
public Node(boolean isEnd){
this.isEnd = isEnd;
next = new TreeMap<>();
}
}
private Node root;
private int size;
public MyTrie(){
root = new Node();
size=0;
}
public int getSize(){
return size;
}
public void add(String word){
Node cur = root;
for(int i =0;i<word.length();i++){
char w = word.charAt(i);
if(!cur.next.containsKey(w))
cur.next.put(w,new Node());
cur = cur.next.get(w);
}
if(!cur.isEnd){
cur.isEnd=true;
size++;
}
}
public Boolean contains(String word){
Node cur = root;
for(int i = 0;i<word.length();i++){
if(cur.next.get(word.charAt(i))==null)
return false;
cur = cur.next.get(word.charAt(i));
}
return cur.isEnd;
}
}