提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
小白理解双数组字典树
在网上也查了很多资料,最后总结一波~
1,怎么建立双数组字典树
2,为什么建立双数组字典树
3,时间复杂度是多少
4,代码实现
一、怎么建立双数组字典树?
双数组字典树是用base数组和check数组共同来建立的,base值加上原来字母的偏移量就是该字母的孩子,但是存在一个问题就是两个父亲的孩子可能相同,所以就用了一个check数组来检查如果相同就重新找base值,这里找base值的时候用的暴力查找。
int getBase(node* root, DAnode* da_tire) {
int ba = 1;
do {
for (int i = 0; i < base; i++) {
if (root->next[i] == NULL)continue;
if (da_tire[ba + i].check == 0)continue;
ba = -ba;
break;
}
if (ba > 0)return ba;
ba = -ba + 1;
} while (1);
return -1;
}
二,传统的字典树空间浪费太严重了,比如k叉树可能就会浪费25n - 1,的空间,所以才用了双数组优化空间,但是在动态插入的时候会浪费很多时间所以也算是时间换空间。
三、时间复杂度
在建立字典树的时间复杂度是O(n)的,同时我们一般在使用这个数据结构的时候都是离线建树,在线查找的。
代码实现
//路飞的代码模板
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
#define base 26
char s[10000];
struct node {
int flag;
struct node* next[base];
};
//构建两个数组进行压缩
struct DAnode {
int ba, check;
};
DAnode da_trie[base + 5];
int da_root = 1;
//初始化根节点
void init_da_trie() {
memset(da_trie, 0, sizeof(da_trie));
da_root = 1;
da_trie[da_root].check = 1;
return;
}
//获取新节点
node* getnode() {
node* p = (node*)malloc(sizeof(node));
p->flag = 0;
memset(p->next, 0, sizeof(node*) * base);
return p;
}
//这里注意,要频繁调用这个函数 所以将此函数改为内敛函数
inline int code(char c) {
return c - 'a';
}
//插入
void insert(node* root, const char *s) {
node* p = root;
for (int i = 0; s[i]; i++) {
int ind = code(s[i]);
if (p->next[ind] == NULL)p->next[ind] = getnode();
p = p->next[ind];
}
p->flag = 1;
return;
}
//使用完之后进行清除操作
void clear(node* root) {
if (root == NULL)return;
for (int i = 0; i < base; i++)clear(root->next[i]);
free(root);
return;
}
//得到该节点的base值
int getBase(node* root, DAnode* da_tire) {
int ba = 1;
do {
for (int i = 0; i < base; i++) {
if (root->next[i] == NULL)continue;
if (da_tire[ba + i].check == 0)continue;
ba = -ba;
break;
}
if (ba > 0)return ba;
ba = -ba + 1;
} while (1);
return -1;
}
//递归处理所有的节点
int transform_trie_double_array(node* root, int ind, DAnode* da_trie) {
if (root == NULL)return ind;
int max_ind = ind;
//确定root节点的ba值
int ba = getBase(root, da_trie);
da_trie[ind].ba = ba;
//标记所有子节点的ba值
for (int i = 0; i < base; i++) {
if (root->next[i] == NULL)continue;
if (root->next[i]->flag)da_trie[ba + i].check = -ind;
else da_trie[ba + i].check = ind;
}
//依次确定所以节点的ba值
for (int i = 0; i < base; i++) {
if (root->next[i] == NULL)continue;
int temp_ind = transform_trie_double_array(root->next[i], ba + i, da_trie);
if (temp_ind > max_ind)max_ind = temp_ind;
}
return max_ind;
}
//最后输出处理之后个节点的信息,可以通过信息还原插入的单词
void output(DAnode* da_trie, int max_ind) {
for (int i = 1; i <= max_ind; i++) {
printf("( %2d | %2d,%3d)\t", i, da_trie[i].ba, da_trie[i].check);
if (i % 5 == 0)cout << endl;
}
cout << endl;
return ;
}
//查找信息
int da_search(DAnode* da_trie, const char* s) {
int p = da_root;
for (int i = 0; s[i]; i++) {
int ind = s[i] - 'a';
if(abs(da_trie[da_trie[p].ba + ind].check) != p)return 0;
p = da_trie[p].ba + ind;
}
return da_trie[p].check < 0;
}
int main() {
init_da_trie();
node* root = getnode();
//insert
while (cin >> s) {
if (strcmp(s,"end") == 0)break;
insert(root, s);
cout << "insert " << s << " done" << endl;
}
int max_ind = transform_trie_double_array(root, da_root, da_trie);
output(da_trie,max_ind);
//search
while (cin >> s) {
if (strcmp(s, "end") == 0)break;
cout << "search " << s << " from double array trie = " << da_search(da_trie, s) << endl;
}
clear(root);
return 0;
}
各位大佬 感谢阅读点个👍吧!