文章目录
1. 前缀树
这棵Trie树包含的字符串集合是{in, inn, int, tea, ten, to}
。每个节点的编号是我们为了描述方便加上去的。树中的每一条边上都标识有一个字符。
比如上图中3号节点对应的路径0123上的字符串是inn
,8号节点对应的路径0568上的字符串是ten
。终结点与集合中的字符串是一一对应的。
前缀树操作
Trie树的创建是从只有根节点开始,通过依次将W1, W2, W3, … WN插入Trie中实现的
Trie一般支持两个操作:
-
Trie.insert(W)
:第一个操作是插入操作,就是将一个字符串W加入到集合中。 -
Trie.search(S)
:第二个操作是查询操作,就是查询一个字符串S是不是在集合中。
插入操作
插入字符串in
。开始位于根,用P=0
表示。先看P是不是有一条标识着i的连向子节点的边。没有这条边就新建一个1号节点,把1号节点设置为P,并且将边标识为i。最后我们移动到1号节点,令P=1
。
插入字符n
,先找P有没有标记为n
的边。还是没有,于是再新建一个节点2,并且把边标识为n
。最后P=2
。由于n
是in
的最后一个字符,所以我们还需要将P=2
这个节点标记为终结点。
再插入字符串inn
。从P=0
开始找标识为i的边,这次找到1号节点。直接移动到1号节点,也就是令P=1
。再插入字符n
,也是有2号节点存在,所以移动到2号节点,P=2
。最后再插入字符n
,这时P没有标识为n
的边了,所以新建3号节点作为2号节点的子节点,边标识为n
,同时将3号节点标记为终结点:
将后面的字符串int tea ten to
都插入之后,就得到了我们一开始给出的Trie。
2. 应用场景
[题目]
一个字符串类型的数组arr1
,另一个字符串类型的数组arr2
,arr2
中有哪些字符,是arr1
中出现的?请打印。
arr2
中有哪些字符,是作为arr1
中某个字符串前缀出现的?请打印。
arr2
中有哪些字符,是作为arr1
中某个字符前缀出现的?请打印arr2
中出现次数最大的前缀。
[算法图解]
2.1 前缀树操作伪代码描述
2.1.1 插入操作
// 初始化root
Initialize: cur = root
// 对于S中每个字符c
for each char c in target string S:
// 如果cur孩子不是c
if cur does not have a child c:
// 新建节点存储
cur.children[c] = new Trie node
// 指向下一个孩子
cur = cur.children[c]
cur is the node which represents the string S
2.1.2 搜索操作
// 初始化root
Initialize: cur = root
// 对于S中每个字符c
for each char c in target string S:
// 如果cur孩子不是c
if cur does not have a child c:
search fails
// 指向下一个孩子
cur = cur.children[c]
search successes
2.2 前缀树操作代码实现
2.2.1 前缀树节点结构
public class TrieNode {
// 节点到达次数
public int path;
// 标记是否到达终点
public int end;
// 数组大小根据实际字符集长度决定
public TrieNode[] children;
// nexts[0] == null 没有走向'a'的路
// nexts[1] != null 有走向'a'的路
// ....
// nexts[25] != null 有走向'z'的路
public TrieNode() {
path = 0;
end = 0;
children = new TrieNode[26];
}
}
2.2.2 前缀树操作
public class Trie {
// 定义根结点,根节点不参与存储
private TrieNode root;
public Trie() {
this.root = new TrieNode();
}
// 插入记录
public void insert(String word) {
// 边界条件
if (word == null) {
return;
}
char[] chs = word.toCharArray();
// node指向root
TrieNode node = root;
int index = 0;
for (int i = 0; i < chs.length; i++) {
// 获取字母字符在数组中的下标
index = chs[i] - 'a';
// 如果该字符第一次出现,则新建一个节点
if (node.children[index] == null) {
node.children[index] = new TrieNode();
}
// node指向下一个位置
node = node.children[index];
// 经过节点次数++
node.path++;
}
// word字符串存储完毕,记录一下位置
node.end++;
}
// 删除记录
public void delete(String word) {
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';
//当前节点的下级节点的path
if (--node.children[index].path == 0) {
//下级节点标空
node.children[index] = null;
return;
}
node = node.children[index];
}
node.end--;
}
}
// 判断记录是否在前缀树中
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.children[index] == null) {
return 0;
}
node = node.children[index];
}
return 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.children[index] == null) {
return 0;
}
node = node.children[index];
}
return node.path;
}
}
3. 图解前缀树
[1/4]
[2/4]
[3/4]
[4/4]