Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构
用于高效地存储和检索字符串数据集中的键
这一数据结构有相当多的应用情景,例如自动补完和拼写检查
//字典树
class Trie {
private class TrieNode{
private boolean isEnd;
private TrieNode[] next;
public TrieNode(){
isEnd = false;
next = new TrieNode[26];
}
}
private TrieNode root;
public Trie() {
root = new TrieNode();
}
public void insert(String word) {
TrieNode curent = root;
int ch;
int n = word.length();
for(int i=0;i<n;i++){
ch = word.charAt(i)-'a';
if(curent.next[ch]==null){
curent.next[ch] = new TrieNode();
}
curent = curent.next[ch];
}
curent.isEnd = true;
}
public boolean search(String word) {
TrieNode curent = root;
int ch;
int n = word.length();
for(int i=0;i<n;i++){
ch = word.charAt(i)-'a';
if(curent.next[ch]==null){
return false;
}
curent = curent.next[ch];
}
return curent.isEnd;
}
public boolean startsWith(String prefix) {
TrieNode curent = root;
int ch;
int n = prefix.length();
for(int i=0;i<n;i++){
ch = prefix.charAt(i)-'a';
if(curent.next[ch]==null){
return false;
}
curent = curent.next[ch];
}
return true;
}
}
应用举例
力扣212. 单词搜索 II (https://leetcode-cn.com/problems/word-search-ii/)
给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words, 返回所有二维网格上的单词 。
单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
示例 1:
输入:board = [[“o”,“a”,“a”,“n”],[“e”,“t”,“a”,“e”],[“i”,“h”,“k”,“r”],[“i”,“f”,“l”,“v”]], words = [“oath”,“pea”,“eat”,“rain”]
输出:[“eat”,“oath”]
示例 2:
输入:board = [[“a”,“b”],[“c”,“d”]], words = [“abcb”]
输出:[]
直接字典树加递归回溯
class Solution {
private Trie tree;
public List<String> res = new ArrayList<>();
int direction[][] ={{1,0},{-1,0},{0,-1},{0,1}};
//boolean [][] visited;
public List<String> findWords(char[][] board, String[] words) {
tree = new Trie();
for(String s:words){
tree.insert(s);
}
int n,m;
n = board.length;
m = board[0].length;
for(int i = 0;i<n;i++){
for(int j=0;j<m;j++){
//visited = new boolean[n][m];
dfs(i,j,board,tree.root);
}
}
return res;
}
public void dfs(int x,int y,char [][] board,Trie.TrieNode cur){
if(x<0||x>=board.length||y<0||y>=board[0].length||board[x][y]=='*'){
return ;
}
cur = cur.next[board[x][y]-'a'];
if(cur==null){
return ;
}
if(cur.isEnd){
cur.isEnd = false;
res.add(cur.value);
}
char temp = board[x][y];
board[x][y]='*';
for(int i=0;i<4;i++){
dfs(x+direction[i][0],y+direction[i][1],board,cur);
}
board[x][y]=temp;
}
private class Trie{
private class TrieNode{
String value;
boolean isEnd;
TrieNode []next;
TrieNode(){
isEnd = false;
next = new TrieNode[26];
}
}
public Trie(){
root = new TrieNode();
}
private TrieNode root;
private void insert(String s){
TrieNode cur = root;
int n = s.length();
int ch;
for(int i =0;i<n;i++){
ch = s.charAt(i)-'a';
if(cur.next[ch]==null){
cur.next[ch] = new TrieNode();
}
cur = cur.next[ch];
}
cur.isEnd = true;
cur.value = s;
}
private boolean isPrefix(String s){
TrieNode cur = root;
int n = s.length();
int ch;
for(int i =0;i<n;i++){
ch = s.charAt(i)-'a';
if(cur.next[ch]==null){
return false;
}
cur = cur.next[ch];
}
return true;
}
private boolean contains(String s){
TrieNode cur = root;
int n = s.length();
int ch;
for(int i =0;i<n;i++){
ch = s.charAt(i)-'a';
if(cur.next[ch]==null){
return false;
}
cur = cur.next[ch];
}
return cur.isEnd;
}
}
}