目录
结点类
包含key和value及一个子节点表,初始化只需要传入key并创建子结点表。
private static class Node{
char key;
String value;
Map<Character, Node> children;
Node(char key){
this.key=key;
this.children = new HashMap<>();
}
}
插入
遍历待插入字符串的每个字符,用curr指向root。如果curr子结点key不包含该字符,则插入新结点,并将curr指向该节点,继续遍历下个字符。遍历结束后,将curr的value设为待插入字符串。
public void insert(String value){
Node cur=root;
for (char c:value.toCharArray()){
if (!cur.children.containsKey(c))
cur.children.put(c,new Node(c));
cur=cur.children.get(c);
}
cur.value=value;
}
删除
遍历待删除字符串,如果遇到value值不为空或有两个及以上子结点的结点,则将res指向该结点,并记录下一个key,遍历结束之后删除res对应key的子结点。
public void delete(String value){
Node res = root,cur=root;
char key = value.charAt(0);
int flag=0;
for (char c:value.toCharArray()){
if (flag==1)key=c;flag=0;
cur=cur.children.get(c);
if (cur.children.size()>=2||cur.value!=null){
if (cur.value!=null)if (cur.value.compareTo(value)==0)break;
res=cur;
flag=1;
}
}
res.children.remove(key);
}
前缀串搜索
遍历前缀至最后一个字符,对该字符对应结点本身及其子树进行遍历,value不为空即输出。
public int dfs(Node root,int flag){
if(root == null) return 0;
if (root.value!=null){System.out.println(root.value);flag=1;}
for(Map.Entry<Character, Node> entry : root.children.entrySet()){
dfs(root.children.get(entry.getKey()),flag);
}
return flag;
}
public void getValue(String prefix){
Node cur=root;
for (char c:prefix.toCharArray()){
cur=cur.children.get(c);
}
int flag=dfs(cur,0);
if (flag==0)System.out.println("Nothing was found.");
}
完整代码及测试主函数
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Trie {
public static void main(String[] args) throws FileNotFoundException {
Trie a=new Trie();
Scanner sc = new Scanner(new File("dictionary.txt"));
while (sc.hasNext()){
a.insert(sc.nextLine());
}
a.delete("aahed");
a.insert("aaa");
a.getValue("aa");
a.getValue("system");
a.getValue("systemm");
}
private static class Node{
char key;
String value;
Map<Character, Node> children;
Node(char key){
this.key=key;
this.children = new HashMap<>();
}
}
private final Node root;
public Trie(){this.root=new Node(' ');}
public void insert(String value){
Node cur=root;
for (char c:value.toCharArray()){
if (!cur.children.containsKey(c))
cur.children.put(c,new Node(c));
cur=cur.children.get(c);
}
cur.value=value;
}
public void delete(String value){
Node res = root,cur=root;
char key = value.charAt(0);
int flag=0;
for (char c:value.toCharArray()){
if (flag==1)key=c;flag=0;
cur=cur.children.get(c);
if (cur.children.size()>=2||cur.value!=null){
if (cur.value!=null)if (cur.value.compareTo(value)==0)break;
res=cur;
flag=1;
}
}
res.children.remove(key);
}
public int dfs(Node root,int flag){
if(root == null) return 0;
if (root.value!=null){System.out.println(root.value);flag=1;}
for(Map.Entry<Character, Node> entry : root.children.entrySet()){
dfs(root.children.get(entry.getKey()),flag);
}
return flag;
}
public void getValue(String prefix){
Node cur=root;
for (char c:prefix.toCharArray()){
cur=cur.children.get(c);
}
int flag=dfs(cur,0);
if (flag==0)System.out.println("Nothing was found.");
}
}