# Dictionary tree and dictionary Tree Code

Source: Internet
Author: User

Dictionary tree concept:

A dictionary tree, as its name implies, is a special data structure that processes letters and other strings. To put it bluntly, it is the 26th tree. Define a header pointer and start operations from the beginning each time.

Dictionary tree analysis:

The hash method can also be used to calculate word frequency, but the dictionary tree is better. The dictionary tree can also calculate the prefix, but it cannot be used in hash.

In extreme cases, each node has 26 letters, And the occupied space is 26 ^ n, where n indicates the average length of words. However, for words, many nodes cannot appear, and some nodes frequently appear, such as the prefix pre of words. In this way, you can use the dictionary tree to quickly search for and insert words and count Word Frequency and prefix, very efficient.

There are two common operations:
1. query the number of occurrences of a string.
The Count of each node is set to 0 until the end of the string. In this way, the Count ++ at the end records the number of occurrences of the string.
2. query the number of times a specific string sequence appears.
The Count of each node is initialized to 0. If one character is read, Count ++. In this way, the Count record of this node is the number of occurrences of the specific sequence from the original node to this node. It can be used to calculate the prefix of a word.

`# Include <string> # include <cstring> # include <cstdlib> # include <cstdio> # include <algorithm> # include <iostream> # include <assert. h> using namespace STD; # define Max 26 // The total number of alphabet is 26,... zstruct dictree {bool word; int count; struct dictree * trie [Max]; // The 26 Child} * A; int Init () // init the chained list {A = new dictree; For (INT I = 0; I <Max; I ++) {A-> trie [I] = NULL; a-> word = false;} Return 0;} bool searchtrie (char * Str) {int Len, Res; dictree * head = A; Assert (head); Len = strlen (STR ); for (INT I = 0; I <Len; I ++) {res = (INT) (STR [I]-'A '); if (Head-> trie [res] = NULL) return false; head = head-> trie [res];} If (Head-> word) return true; return false;} int inserttrie (char * Str) {int Len, Res; dictree * head = A; Len = strlen (STR); For (INT I = 0; I <Len; I ++) {res = int (STR [I]-'A'); If (Head-> Trie [Res] = NULL) // whether the node exist? {Head-> trie [res] = new dictree; head = head-> trie [res]; head-> COUNT = 0; For (Int J = 0; j <Max; j ++) {head-> trie [J] = NULL; head-> word = false;} elsehead = head-> trie [res];} head-> count ++; head-> word = true; return head-> count;} int main () {char STR [20]; Init (); for (INT I = 0; I <10; I ++) {scanf ("% s", STR); printf ("% d \ n ", inserttrie (STR);} scanf ("% s", STR); printf ("% s \ n", searchtrie (STR )? ("Yes") :( "no"); Return 0 ;}`

PS: No bool word is added to the dictree In the first version. The word search is inaccurate, if the current node WORD = true, it indicates that it is a word from the root node to the node. If it is false, it indicates that the node is in the middle of the word.

Related Keywords:

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.