1、单词查找树(前缀树)
又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种。典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高。 ----百度
具体图文表述请看别的技术博客,这里我分别用数组和map来表示树的下一个节点,具体代码实现和细节如下:
数组实现:需要一开始就初始化,比如有26个字母,就将下一个节点集合变成大小为26的节点数字
package cn.nupt;
/**
* @Description: 前缀树(单词查找树),数组表示
*
* @author PizAn
* @Email [email protected]
* @date 2019年3月6日 下午3:32:33
*
*/
public class TrieTree {
static class TrieNode {
//节点类,就跟链表、二叉树一样
private int path; //经过这个节点的路径 的数目,用来计算前缀数
private int end; //以这个节点为尾的字符串的数目
private TrieNode[] next; //指向下一个节点的指针(就跟链表一样)
public TrieNode() {
path = 0;
end = 0;
next = new TrieNode[26]; //因为是a~z,所以这里初始化为26
}
}
// 默认初始化一个头节点
public static class Trie {
//这里再来一个内部类来放方法
private TrieNode root;
public Trie() {
root = new TrieNode();
}
/**
* @Description: 压入字符串
* @param word
*
*/
public void insert(String word) {
if (word == null) {
return;
}
TrieNode node = root;
int index = 0;
char[] cha = word.toCharArray();
for (int i = 0; i < cha.length; i++) {
index = cha[i] - 'a';
// 每一个点都可以对应26个方向,而这个步骤是找到这个方向(index)看这个方向有没有东西,没有的话就创建一个
// 有的话就直接跳到下一个
if (node.next[index] == null) {
node.next[index] = new TrieNode();
}
node = node.next[index];
node.path++; // 将路径记录+1
}
node.end++; // 在最后一个节点记录一下
}
/**
* @Description: 查找字符串,并返回字符串个数
* @param word
* @return
*
*/
public int search(String word) {
if (word == null) {
return 0;
}
TrieNode node = root;
int index = 0;
char[] cha = word.toCharArray();
for (int i = 0; i < cha.length; i++) {
index = cha[i] - 'a';
if (node.next[index] == null) {
//如果找到下一个节点为空,直接返回,说明没有这个单词
return 0;
}
node = node.next[index];
}
return node.end;
}
/**
* @Description: 删除字符串(就是