最近了解es相关知识,发现term index是一棵trie树,于是参考网上blog,实现了一下trie数,发现这个数据结构非常实用。特记载于此。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;

#define SIZE 26

typedef struct trie_node
{
int count;
trie_node* childs[SIZE];
}* trie;

trie create()
{
trie_node* node = new trie_node();
node->count = 0;
for(int i=0;i<SIZE;i++)
{
node->childs[i]=NULL;
}
return node;
}

void insert(trie root, char* k)
{
trie_node* node = root;
char* p = k;
while(*p)
{
if(node->childs[*p-'a'] == NULL)
{
node->childs[*p-'a'] = create();
}
node = node->childs[*p-'a'];
node->count +=1;
++p;
}

}

int search(trie root, char* k)
{
trie_node* node = root;
char* p = k;
while(*p && node!=NULL)
{
node = node->childs[*p-'a'];
++p;
}
if(node == NULL)
return 0;
else
return node->count;
}

int main()
{
char keys[][8]={"the", "a", "there", "answer", "any", "by", "bye", "their"};
trie root = create();
for(int i=0;i<8;i++)
insert(root, keys[i]);

printf("%s --- %d\n", "the", search(root, "the"));
printf("%s --- %d\n", "these", search(root, "these"));
printf("%s --- %d\n", "their", search(root, "their"));
printf("%s --- %d\n", "thaw", search(root, "thaw"));
printf("%s --- %d\n", "a", search(root, "a"));
printf("%s --- %d\n", "b", search(root, "b"));
return 0;
}

参考

1 elasticsearch 倒排索引原理
2 Trie树(Prefix Tree)介绍这篇文章的代码貌似有点问题,看思路即可。