前缀树
Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少无谓的字符串比较,查询效率比哈希表高。
注:字符都在路径上,节点上没有值。
它有3个基本性质:
(1)根节点不包含字符,除根节点外每一个节点都只包含一个字符。
(2)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
(3)每个节点的所有子节点包含的字符都不相同。
public class hello {
//前缀树节点
public static class TrieNode {
public int path; //记录有多少个节点到达
public int end; //记录有多少字符串以这个节点结尾
public TrieNode[] nexts; //记录该节点的所有路径
public TrieNode() {
path = 0;
end = 0;
nexts = new TrieNode[26];//每个节点有26条路径(a,b,...)
}
}
//前缀树
public static class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
//插入字符串word
public void insert(String word) {
if (word == null) {
return;
}
//将字符串转换为数组
char[] chs = word.toCharArray();
TrieNode node = root;
//节点路径下标
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';//index值为0-25
if (node.nexts[index] == null) {
node.nexts[index] = new TrieNode();
}
//添加一个新节点,新节点到达一次
node = node.nexts[index];
node.path++;
}
//以该节点结尾,+1
node.end++;
}
//查询是否有字符串word
public int search(String word) {
if (word == null) {
return 0;
}
char[] chs = word.toCharArray();
TrieNode node = root;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.end;
}
//删除字符串word
public void delete(String word) {
//查询word不为0的前提
if (search(word) != 0) {
char[] chs = word.toCharArray();
TrieNode node = root;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
//如果该路径只有一条,则直接删去并且该节点之后也不需要考虑
if (--node.nexts[index].path == 0) {
node.nexts[index] = null;
return;
}
node = node.nexts[index];
}
node.end--;
}
}
//计算字符串pre作为多少个字符串的前缀
public int prefixNumber(String pre) {
if (pre == null) {
return 0;
}
char[] chs = pre.toCharArray();
TrieNode node = root;
int index = 0;
for (int i = 0; i < chs.length; i++) {
index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.path;
}
}
public static void main(String[] args) {
Trie trie = new Trie();
System.out.println(trie.search("cao"));
trie.insert("cao");
System.out.println(trie.search("cao"));
trie.delete("cao");
System.out.println(trie.search("cao"));
System.out.println(trie.prefixNumber("cao"));
}
}