What is a dictionary tree?
Trie I think more should be called a prefix tree, because to use it to seek a string prefix very convenient.
Through the above this figure, we find that:
1, with the edges represent the letter trie
2, a common prefix with the word nodes with the same prefix (It is for this reason we have to find a prefix string is very convenient)
3, the root of the whole tree is empty.
4, the end of each word is represented by a special character, figure by black , then from the root section point to any intended a black circleAfter all the edges of the letters represents a word.
Basic operation:
First, insert
Suppose we want to insert the string "in". We start at the root, i.e. node 0, P = 0 we expressed. We look at P Is there a logo with edge i connected to child nodes. Without this side, so we create a new node, i.e. node No. 1, No. 1 and the node P is set to 0 sub-node number of the node, and the edge identified as i. Finally, we move to the 1st node, that is, so P = 1.
So we put the "in" characters into the i in the Trie. Then we insert characters n, P is first find a node with a number that is not marked as the n side. Or not, then a further new node 2, is set as P 1 sub-node number of the node, and identify the side is n. And then finally moved to P = 2. So we put n also inserted. Since n is "in" the last character, so we also need to P = 2 this node is marked as the end point.
Now we insert the string "inn". The process is the same, from P = 0 i started looking logo for the side, this time to find the No. 1 node. So we do not need to create a new node, and move directly to the node No. 1, that is, so P = 1. Then insert the character n, is the presence of node No. 2, No. 2 to the mobile node, P = 2. Finally, insert the character is not identified as P n time n side, so the new node as the No. 3 No. 2 node's children, identified as the n side, while the serial number of the endpoint node 3:
Repeat, we can get the best view of the start up!
Second, look for
既然插入已经知道了,那么我们现在就来看看查找吧
查找其实比较简单。我们只要从根节点开始,沿着标识着S[1] -> S[2] -> S[3] … -> S[S.len]的边移动,如果最后成功到达一个终结点,就说明S在Trie树中;如果最后无路可走,或者到达一个不是终结点的节点,就说明S不在Trie树中。
如果是查找”te”,就会从0开始经过5最后到达6。但是6不是终结点,所以te没在Trie树中。如果查找的是”too”,就会从0开始经过5和9,然后发现之后无路可走:9号节点没有标记为o的边连出去。所以too也不在Trie中。
代码实现:
数组方式实现
要写代码实现一个Trie首先就要确定如何存储一个Trie结构。这里用一个二维数组来存储:
int trie[MAX_NODE][CHARSET];
int k;
其中MAX_NODE是trie中最大能存储的节点数目,CHARSET是字符集的大小,k是当前trie中包含有多少个节点。Trie[i][j]的值是0表示trie树中i号节点,并没有一条连出去的边,满足边上的字符标识是字符集中第j个字符(从0开始);trie[i][j]的值是正整数x表示trie树中i号节点,有一条连出去的边,满足边上的字符标识是字符集中第j个字符,并且这条边的终点是x号节点。
具体代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <string> 4 #include <iostream> 5 #include <stdlib.h> 6 #include <algorithm> 7 8 using namespace std; 9 const int MAX_NODE = 300001; 10 const int CHARSET = 26; 11 12 int trie[MAX_NODE][CHARSET]={0}; 13 int color[MAX_NODE]={0}; 14 int k = 1; 15 16 void insert(char *w) 17 { 18 int len = strlen(w); 19 int p = 0; 20 for (int i=0;i<len;i++) 21 { 22 int c = w[i]-'a'; 23 if (!trie[p][c]) 24 { 25 trie[p][c] = k; 26 k++; 27 } 28 p = trie[p][c]; 29 } 30 color[p] = 1; 31 } 32 33 34 int search(char *s) 35 { 36 int len = strlen(s); 37 int p = 0; 38 for (int i=0;i<len;i++) 39 { 40 int c = s[i]-'a'; 41 if (!trie[p][c]) 42 return 0; 43 p = trie[p][c]; 44 } 45 return color[p] == 1; //是不是其中的一个 46 } 47 48 49 int main() 50 { 51 #ifndef ONLINE_JUDGE 52 freopen("../in.txt","r",stdin); 53 #endif 54 int t,q; 55 char s[20]; 56 scanf("%d%d", &t,&q); 57 while(t--){ 58 scanf("%s", s); 59 insert(s); 60 } 61 while(q--){ 62 scanf("%s", s); 63 if(search(s)) printf("YES\n"); 64 else printf("NO\n"); 65 } 66 return 0; 67 }
参考博客:https://blog.csdn.net/weixin_39778570/article/details/81990417
https://www.cnblogs.com/TheRoadToTheGold/p/6290732.html