每个节点都从root根节点开始遍历,如果经过某个节点,就将这个节点的pass++,如果一个字符串到达了末尾,就将这个节点的end++;循环遍历每个节点,就会创建出一颗前缀树
public class TrieTree {
public static class Node {
public int pass;
public int end;
public Node[] nexts;
public Node() {
pass = 0;
end = 0;
// 0 a
// 1 b
// 2 c
// .. ..
// 25 z
// nexts[i] == null i方向的路不存在
// nexts[i] != null i方向的路存在
nexts = new Node[26];
}
}
public static class Trie {
// 头结点
private Node root;
// 初始化线段树
public Trie() {
root = new Node();
}
public void insert(String word) {
if (word == null) {
return;
}
char[] str = word.toCharArray();
Node node = root;
// 经过头结点,头结点pass++
node.pass++;
// path所记录的就是当前子节点索引
int path = 0;
for (int i = 0; i < str.length; i++) { // 从左往右遍历字符
path = str[i] - 'a'; // 由字符,对应成走向哪条路
if (node.nexts[path] == null) {
// 新节点直接创建
node.nexts[path] = new Node();
}
// node 下移,所在位置经过 pass++
node = node.nexts[path];
node.pass++;
}
// 遍历到最后,node当前字符串终点,end++
node.end++;
}
public void delete(String word) {
if (search(word) == 0) {
return;
}
char[] str = word.toCharArray();
Node node = root;
node.pass--;
int path = 0;
for (int i = 0; i < str.length; i++) {
path = str[i] - 'a';
// 如果当前节点后面的对应路径pass为空,表示没有字符串经过,后面直接全部置为空即可
if (--node.nexts[path].pass == 0) {
node.nexts[path] = null;
return;
}
node = node.nexts[path];
}
// 如果遍历到最后,其他字符串也经过了这条路径,pass不为0,则将end--,表明当前字符串已经删除,不再是结尾
node.end--;
}
// word这个单词之前加入过几次
public int search(String word) {
if (word == null) {
return 0;
}
char[] str = word.toCharArray();
Node node = root;
int path = 0;
for (int i = 0; i < str.length; i++) { // 从左往右遍历字符
path = str[i] - 'a'; // 由字符,对应成走向哪条路
// 如果下标位置为空,说明不存在这个字符,即这个字符串没有加入过
if (node.nexts[path] == null) {
return 0;
}
// node 下移
node = node.nexts[path];
}
// 遍历到最后,说明找到了word这个字符串,end表示的就是他加入的次数
return node.end;
}
// 所有加入的字符串中,有几个是以pre这个字符串作为前缀的
public int startsWith(String prefix) {
if (prefix == null) {
return 0;
}
char[] str = prefix.toCharArray();
Node node = root;
int path = 0;
for (int i = 0; i < str.length; i++) { // 从左往右遍历字符
path = str[i] - 'a'; // 由字符,对应成走向哪条路
// 如果下标位置为空,说明不存在这个字符,即这个前缀字符串没有加入过
if (node.nexts[path] == null) {
return 0;
}
// node 下移
node = node.nexts[path];
}
// 遍历到最后,说明找到了word这个字符串,end表示的就是他加入的次数
return node.pass;
}
}
}
LeetCode 208. 实现 Trie (前缀树)
class Trie {
public static class Node {
public int pass;
public int end;
public Node[] nexts;
public Node() {
pass = 0;
end = 0;
// 0 a
// 1 b
// 2 c
// .. ..
// 25 z
// nexts[i] == null i方向的路不存在
// nexts[i] != null i方向的路存在
nexts = new Node[26];
}
}
// 头结点
private Node root;
// 初始化线段树
public Trie() {
root = new Node();
}
public void insert(String word) {
if (word == null) {
return;
}
char[] str = word.toCharArray();
Node node = root;
// 经过头结点,头结点pass++
node.pass++;
// path所记录的就是当前子节点索引
int path = 0;
for (int i = 0; i < str.length; i++) { // 从左往右遍历字符
path = str[i] - 'a'; // 由字符,对应成走向哪条路
if (node.nexts[path] == null) {
// 新节点直接创建
node.nexts[path] = new Node();
}
// node 下移,所在位置经过 pass++
node = node.nexts[path];
node.pass++;
}
// 遍历到最后,node当前字符串终点,end++
node.end++;
}
// word这个单词之前加入过几次
public boolean search(String word) {
if (word == null) {
return true;
}
char[] str = word.toCharArray();
Node node = root;
int path = 0;
for (int i = 0; i < str.length; i++) { // 从左往右遍历字符
path = str[i] - 'a'; // 由字符,对应成走向哪条路
// 如果下标位置为空,说明不存在这个字符,即这个字符串没有加入过
if (node.nexts[path] == null) {
return false;
}
// node 下移
node = node.nexts[path];
}
// 遍历到最后,说明找到了word这个字符串,end表示的就是他加入的次数
return node.end != 0;
}
public boolean startsWith(String prefix) {
if (prefix == null) {
return true;
}
char[] str = prefix.toCharArray();
Node node = root;
int path = 0;
for (int i = 0; i < str.length; i++) { // 从左往右遍历字符
path = str[i] - 'a'; // 由字符,对应成走向哪条路
// 如果下标位置为空,说明不存在这个字符,即这个前缀字符串没有加入过
if (node.nexts[path] == null) {
return false;
}
// node 下移
node = node.nexts[path];
}
// 遍历到最后,说明找到了word这个字符串,end表示的就是他加入的次数
return node.pass != 0;
}
}



