Dictionary tree C ++

Source: Internet
Author: User

Dictionary tree C ++ implementation

Dictionary tree:

The word search tree, also known as the trie tree, is a tree structure and a variant of the hash tree. A typical application is used for statistics, sorting, and saving a large number of strings (but not limited to strings). Therefore, it is often used by the search engine system for text word frequency statistics. It saves storage space by using the public prefix of strings and minimizes unnecessary string comparisons. The query efficiency is higher than that of hash tables.

The basic function of the dictionary tree is to query a Data Structure of the number of occurrences of a word (prefix) in all words. Its insertion and query complexity are O (LEN ), len is the length of a word (prefix), but its spatial complexity is very high. If the character set is 26 letters, each node has 26 degrees, A typical time structure is space-based.

The data structure of the storage dictionary Tree node is as follows:

Typedef struct Node
Struct node * Next [Max]; // indicates that each node has a maximum of 26 child nodes.
Int num; // indicates the number of stored child nodes.
} Node;

The following example uses 100000 words with a length not greater than 10. For every word, we need to determine whether it appears.

If the word I want to query is ABCD, I obviously don't have to consider the words that start with B, C, D, and F. However, you only need to check whether there is ABCD in the name starting with. Similarly, in words starting with a, we only need to consider B as the second letter ...... This tree model is becoming clearer ......

Dictionary Tree Code implementation:

# Include <iostream> using namespace STD; # define Max 26 typedef struct node {struct node * Next [Max]; int num;} node; // create a new node * createnew () {node * P = new node; For (INT I = 0; I <Max; I ++) {P-> next [I] = NULL;} p-> num = 0; return P;} // insert a string void insert_str (char STR [], node * head) {int Len = strlen (STR); node * t, * P = head; For (INT I = 0; I <Len; I ++) {int c = STR [I]-'A'; If (p-> next [c] = NULL) {T = createnew (); p-> next [c] = T; P-> num ++; // cout <p-> num <Endl; P = p-> next [c];} else {P = p-> next [c] ;}} int search_str (char STR [], node * head) {node * P = head; int Len = strlen (STR); int COUNT = 0; For (INT I = 0; I <Len; I ++) {int c = STR [I]-'A'; If (p-> next [c] = NULL) {cout <"no string" <Endl; count = 0; return 0;} else {P = p-> next [c]; Count = p-> num ;}} return count ;} int main () {cout <"nihao" <Endl; node * head = createnew (); char s [10]; while (CIN> S, strcmp (S, "quit") {insert_str (S, head);} int c = search_str ("ABC", head); cout <C <Endl; system ("pause"); Return 0 ;}

Typical application of the dictionary tree:

1. count the number of times a prefix of a string appears (just use the code above ).

2. Check whether one string in A group is the prefix of another string.

Analysis: we only need to add an nendflag member variable to the node. If nendflag = 1, it indicates that the character at this node is the end of a string (assumed as a). If the string is inserted through this node, it indicates that a is the prefix of B. In another case, when the last character C is to be inserted, it is found that P-> next [C-'a'] is true, it indicates that this string is a prefix string. For example, insert ABCDE first and then insert ABC.

3. String sorting: given n English names that are different from each other and are composed of only one word, you can output them in lexicographically ascending order.

Sort by dictionary tree and create a dictionary tree by array. All the sons of each node of the tree are obviously sorted by their letter size. Traverse the tree in sequence.


Source Address: http://www.cnblogs.com/dlutxm/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.