Dictionary tree C ++ implementation

Source: Internet
Author: User

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
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.


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.