一、前缀树概述
前缀树又名字典树,单词查找树,Trie树,是一种多路树形结构,是哈希树的变种,和hash效率有一拼,是一种用于快速检索的多叉树结构。
典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度的减少无谓的字符串比较,查询效率比哈希表高。
Trie树的核心思想是空间换时间。利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。Trie树也有它的缺点,那就是内存消耗非常大。
二、应用场景
我用一个引例,来聊聊前缀树的具体应用场景。
给定一个字符串类型的数组arr1,另一个字符串类型的数组arr2。
1.arr2中有哪些字符,是arr1中出现的?请打印。
2.arr2中有哪些字符,是作为arr1中某个字符串前缀出现的?请打印。
3.arr2中有哪些字符,是作为arr1中某个字符串前缀出现的?请打印arr2中出现次数最大的前缀。
对于上述问题,我们来看看前缀树是怎么解决的
三、前缀树原理
前缀树将相同前缀的单词做为公共用的前缀,如果找不到这个前缀,则在另外一个结点上插入这个单词。并且在这个单词结尾的结点加上1。
四、前缀树实现
#include<iostream>
using namespace std;
class TrieNode {
public:
int pass;//经过该结点的次数
int end;//以该结点做单词末尾的次数
TrieNode* next[26];//默认处理的单词为从a到z
TrieNode() {
pass = 0;
end = 0;
for (int i = 0; i < 26; i++) {
next[i] = NULL;
}
}
~TrieNode() {
for (int i = 0; i < 26; i++) {
if(next[i])
delete next[i];
}
}
};
class Trie {
private:
TrieNode* root;
public:
Trie() {
root = new TrieNode;
}
void insert(string word);//将word添加到字典树
int search(string word);//word在字典树里被添加了多少次
bool remove(string word);//将word从字典树中删除
int startWith(string word);//字典树中有多少单词是以word为前缀的
};
void Trie::insert(string word) {
if (!word.size())return;
TrieNode* node = this->root;
int index = 0;
for (int i = 0; i < word.size(); i++) {
index = word[i] - 'a';
if (!node->next[index]) {
node->next[index] = new TrieNode;
}
node = node->next[index];
node->pass++;
}
node->end++;
}
int Trie::search(string word) {
if (!word.size()) return 0;
TrieNode* node = this->root;
int index = 0;
for (int i = 0; i < word.size(); i++) {
index = word[i] - 'a';
if (!node->next[index]) {
return 0;//没有后续的单词字母,直接返回
}
node = node->next[index];
}
return node->end;
}
bool Trie::remove(string word) {
if (!search(word))return false;
TrieNode* node = root;
int index = 0;
for (int i = 0; i < word.size(); i++) {
index = word[i] - 'a';
if (--node->next[index]->pass == 0) {
delete node->next[index];
node->next[index] = NULL;
return true;
}
node = node->next[index];
}
node->end--;
return true;
}
int Trie::startWith(string word) {
TrieNode* node = this->root;
int index = 0;
for (int i = 0; i < word.size(); i++) {
index = word[i] - 'a';
if (!node->next[index]) {
return 0;
}
node = node->next[index];
}
return node->pass;
}
五、Leetcode实战
Leetcode.208 实现Trie(前缀树)
class TrieNode{
public:
TrieNode*next[26];
bool isWord;
TrieNode(){
for(int i=0;i<26;i++){
next[i]=NULL;
}
isWord=false;
}
};
class Trie {
public:
TrieNode*root;
Trie() {
root=new TrieNode;
}
void insert(string word) {
TrieNode*node=this->root;
int index=0;
for(int i=0;i<word.size();i++){
index=word[i]-'a';
if(node->next[index]==NULL){
node->next[index]=new TrieNode;
}
node=node->next[index];
}
node->isWord=true;
}
bool search(string word) {
TrieNode*node=this->root;
int index=0;
for(int i=0;i<word.size();i++){
index=word[i]-'a';
if(!node->next[index]) return false;
node=node->next[index];
}
return node->isWord;
}
bool startsWith(string prefix) {
TrieNode*node=this->root;
int index=0;
for(int i=0;i<prefix.size();i++){
index=prefix[i]-'a';
if(!node->next[index]) return false;
node=node->next[index];
}
return true;
}
};
Leetcode.211.添加与搜索单词-数据结构设计
class WordDictionary {
public:
bool isWord;
WordDictionary*next[26];
WordDictionary() {
isWord=false;
for(int i=0;i<26;i++){
next[i]=NULL;
}
}
void addWord(string word) {
WordDictionary*node=this;
for(int i=0;i<word.size();i++){
if(!node->next[word[i]-'a'])
node->next[word[i]-'a']=new WordDictionary;
node=node->next[word[i]-'a'];
}
node->isWord=true;
}
bool search(string word) {
WordDictionary*node=this;
for(int i=0;i<word.size();i++){
if(word[i]=='.'){
for(int j=0;j<26;j++){
if(node->next[j]){
if(node->next[j]->search(word.substr(i+1,word.size()-i-1)))
return true;
}
}
return false;
}
else{
if(!node->next[word[i]-'a']) return false;
node=node->next[word[i]-'a'];
}
}
return node->isWord;
}
};