链表实现
package com.company.test.字典树的实现;
/**
* @author xienl
* @description 字典树的实现
* @date 2022/8/23
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
public String[] trieU (String[][] operators) {
// write code here
int len = 0;
for (String[] operator : operators) {
if (operator[0].equals("3") || operator[0].equals("4")){
len++;
}
}
Trie trie = new Trie();
String[] res = new String[len];
int index = 0;
for (String[] operator : operators) {
if (operator[0].equals("1")){
trie.insert(operator[1]);
}
if (operator[0].equals("2")){
trie.delete(operator[1]);
}
if (operator[0].equals("3")){
boolean search = trie.search(operator[1]);
res[index++] = search ? "YES" : "NO";
}
if (operator[0].equals("4")){
int num = trie.prefixNumber(operator[1]);
res[index++] = Integer.toString(num);
}
}
return res;
}
}
/**
* 前缀树,链表实现
*/
class Trie{
/**
* 节点对象
*/
class TrieNode{
int num;
boolean end;
TrieNode[] child;
public TrieNode(){
this.num = 0;
this.end = false;
this.child = new TrieNode[26];
}
}
TrieNode root = new TrieNode();
/**
* 增加
* @param word
*/
void insert(String word){
TrieNode node = root;
char[] chars = word.toCharArray();
for (int i = 0; i < chars.length; i++){
char c = word.charAt(i);
if (node.child[c - 'a'] == null){
node.child[c - 'a'] = new TrieNode();
}
node = node.child[ c - 'a'];
node.num++;
}
node.end = true;
}
/**
* 删除
* @param word
*/
void delete(String word){
TrieNode node = root;
char[] chars = word.toCharArray();
for (char c : chars){
node = node.child[c - 'a'];
node.num--;
}
node.end = false;
}
/**
* 查找
* @param word
* @return
*/
boolean search(String word){
TrieNode node = root;
char[] chars = word.toCharArray();
for (char c : chars) {
if (node.child[c - 'a'] == null){
return false;
}
node = node.child[c - 'a'];
}
return node.end;
}
int prefixNumber(String pre){
TrieNode node = root;
char[] chars = pre.toCharArray();
for (char c : chars) {
if (node.child[c - 'a'] == null){
return 0;
}
node = node.child[c - 'a'];
}
return node.num;
}
}
数组实现
package com.company.test.字典树的实现;
/**
* @author xienl
* @description 字典数数组实现
* @date 2022/8/23
*/
public class Solution2 {
public static void main(String[] args) {
Solution solution = new Solution();
}
public String[] trieU (String[][] operators) {
// write code here
int len = 0;
for (String[] operator : operators) {
if (operator[0].equals("3") || operator[0].equals("4")){
len++;
}
}
Trie2 trie = new Trie2();
String[] res = new String[len];
int index = 0;
for (String[] operator : operators) {
if (operator[0].equals("1")){
trie.insert(operator[1]);
}
if (operator[0].equals("2")){
trie.delete(operator[1]);
}
if (operator[0].equals("3")){
boolean search = trie.search(operator[1]);
res[index++] = search ? "YES" : "NO";
}
if (operator[0].equals("4")){
int num = trie.prefixNumber(operator[1]);
res[index++] = Integer.toString(num);
}
}
return res;
}
}
class Trie2 {
int n = 700000;
// 字典数
int[][] tire;
// 当前行数
int[] cnt;
// 是否结尾
int[] end;
int index;
public Trie2(){
tire = new int[n][26];
cnt = new int[n];
end = new int[n];
index = 0;
}
void insert(String word){
int cur = 0;
char[] chars = word.toCharArray();
for (char c : chars) {
if (tire[cur][c - 'a'] == 0){
tire[cur][c - 'a'] = ++index;
}
cur = tire[cur][c - 'a'];
cnt[cur]++;
}
end[cur]++;
}
void delete(String word){
int cur = 0;
char[] chars = word.toCharArray();
for (char c : chars) {
cur = tire[cur][c - 'a'];
cnt[cur]--;
}
end[cur]--;
}
boolean search(String word){
int cur = 0;
char[] chars = word.toCharArray();
for (char c : chars) {
if (tire[cur][c - 'a'] == 0){
return false;
}
cur = tire[cur][c - 'a'];
}
return end[cur] > 0;
}
int prefixNumber(String pre){
int cur = 0;
char[] chars = pre.toCharArray();
for (char c : chars) {
if (tire[cur][c - 'a'] == 0){
return 0;
}
cur = tire[cur][c - 'a'];
}
return cnt[cur];
}
}