Introduction to trie tree and its implementation; Introduction to trie tree implementation
Definition: Also known as the dictionary tree, word search tree or Prefix Tree, is a multi-tree structure for quick search,
For example, the English letter dictionary tree is a 26-tree, and the number dictionary tree is a 10-tree.
Core Ideas:Is space for time. Use the public prefix of the string to reduce the overhead of the query time to improve efficiency.
Three basic properties:
1. The root node does not contain characters. Each node except the root node contains only one character.
2. From the root node to a node, the character passing through the path is connected to the string corresponding to the node.
3. All the sub-nodes of each node contain different characters.
Advantages:The common prefix of a string is used to save storage space and minimize unnecessary string comparisons. The query efficiency is higher than that of a hash table.
Disadvantages:If a large number of strings exist and these strings have no common prefixes, the corresponding trie tree will consume a lot of memory.
Typical applications:Statistics and sorting of a large number of strings (but not limited to strings) are often used by the search engine system for text word frequency statistics.
For the implementation of the Trie tree, you can use arrays to allocate space statically, or use pointers to dynamically allocate space.
Trie Tree operations
There are three main operations in the Trie tree: insert, search, and delete. Generally, a single node is rarely deleted in the Trie tree. Therefore, you only need to delete the entire tree.
Assume that the str string (all lowercase letters) exists, and the root node of the Trie tree is root. I = 0, p = root.
Typedef struct stu {int n, flag; // n records the prefix and number of words, flag indicates whether the word exists in struct stu * next [26]; // subnode} node;
Open up new nodes and initialize them:
node* creat_node(){ node *p=(node *)malloc(sizeof(node)); p->n=p->flag=0; memset(p->next,0,sizeof(p->next)); return p;}
1. Insert
1) Take str [I] and judge whether p-> next [str [I]-'a'] is null. If it is null, create the node temp, and point p-> next [str [I]-'a'] to temp, and then p to temp;
If not empty, p = p-> next [str [I]-'a'];
2) I ++, continue taking the operations in str [I], loop 1) until the terminator '\ 0' is encountered, then set the flag in the current node p to true.
Insert and count a string
void trie_insert(node *p,char *s){ int i; while(*s!='\0'){ i=*s-'a'; if(p->next[i]==0) p->next[i]=creat_node(); p=p->next[i]; s++; p->n++; } p->flag=1;}
2. Search
1) Take str [I] and judge whether p-> next [str [I]-'a'] is null. If it is null, false is returned. If it is not null, then p = p-> next [str [I]-'a'] continues to take the character.
2) Repeat the operation in 1) until the terminator '\ 0' is encountered. If the current node p is not empty and the flag is true, true is returned; otherwise, false is returned.
Check whether a string exists and return the number of strings:
int trie_search(node *p,char *s){ int i; while(*s!='\0'){ i=*s-'a'; p=p->next[i]; if(p==0) return 0; s++; } return p->n;}
3. Delete
Deletion can be performed recursively.
Recursively Delete the entire tree:
Void trie_del (node * root) {int I; for (I = 0; I <M; I ++) // M is the number of subnodes if (root-> next [I]! = NULL) trie_del (root-> next [I]); free (root );}
Implementation of the Trie tree
Implementation of the Trie tree!
I know this problem.
We recommend that you use "Sisi QQ stranger promotion assistant"
Send messages directly without adding friends
No problem
Trie tree C Language
Baike.baidu.com/view/1436495.htm you can understand the definition and then you will know.