什么是Trie字典树
- 字典
- 如果有n个条目
- 使用树结构
- 查询的时间复杂度是O(logn)
- 如果有100万个条目(2²º)
- log大约为20
- Trie
- 查询每个条目的时间复杂度
- 和字典中一共有多少条目无关
- 时间复杂度为O(w)
- w为查询单词的长度
- 大多数单词的长度小于10
每个节点有26个指向下个节点的指针
二、Trie字典树基础
package Trie;
import java.util.TreeMap;
public class Trie {
private class Node {
public boolean isWord;
public TreeMap<Character, Node> next;
public Node(boolean isWord) {
this.isWord = isWord;
next = new TreeMap<>();
}
public Node() {
this(false);
}
}
private Node root;
private int size;
public Trie() {
root = new Node();
size = 0;
}
// 获得Trie中存储的单词数量
public int getSize() {
return size;
}
// 向Trie中添加一个新的单词word
public void add(String word) {
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (cur.next.get(c) == null) {
cur.next.put(c, new Node());
}
cur = cur.next.get(c);
}
if (!cur.isWord){
cur.isWord = true;
size++;
}
}
}
三、查询是否在Trie中有单词以prefix为前缀
// 查询是否在Trie中有单词以prefix为前缀
public boolean isPrefix(String prefix) {
Node cur = root;
for (int i=0;i<prefix.length();i++){
char c = prefix.charAt(i);
if (cur.next.get(c)==null){
return false;
}
cur = cur.next.get(c);
}
return true;
} 四、211. 添加与搜索单词 - 数据结构设计
package Trie;
import java.util.TreeMap;
public class WordDictionary {
private class Node {
public boolean isWord;
public TreeMap<Character, Node> next;
public Node(boolean isWord) {
this.isWord = isWord;
next = new TreeMap<>();
}
public Node() {
this(false);
}
}
private Node root;
public WordDictionary() {
root = new Node();
}
public void addWord(String word) {
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (cur.next.get(c) == null) {
cur.next.put(c, new Node());
}
cur = cur.next.get(c);
}
if (!cur.isWord) {
cur.isWord = true;
}
}
public boolean search(String word) {
return match(root, word, 0);
}
public boolean match(Node node, String word, int index) {
if (index == word.length()) {
return node.isWord;
}
char c = word.charAt(index);
if (c != '.') {
if (node.next.get(c) == null) {
return false;
}
return match(node.next.get(c), word, index + 1);
}else {
for (char nextChar: node.next.keySet()){
if (match(node.next.get(nextChar),word,index+1)){
return true;
}
}
return false;
}
}
}
五、Trie的局限性
本文作者为DBC,转载请注明。





