Dictionary tree (Trie)

Source: Internet
Author: User

Dictionary tree (Trie)
Zookeeper

Dictionary tree, also known as Trie, is a multi-tree structure for fast retrieval. Trie regards the keywords to be searched as a character sequence, and constructs the tree structure for retrieval based on the order in which the keywords are composed. A m-degree Trie tree or is empty, or it may consist of m-level Trie trees.

Note: Unlike the Binary Search Tree, its nodes do not store an element.

Advantages: 1. Use public memory to save memory

2. The root node only stores its Subtrees, not letters

3. Each node represents a different letter

Basic definition:

Typedef struct Node {struct Node * child [26]; int n; // changed according to question} Trie;


Time Complexity: Limit n (although the complexity of the binary search tree is the same as limit n, the efficiency is slightly better than that of the Binary Search Tree)

Structure of the dictionary tree:

Related questions:

Hdu1251 statistical difficulties

Hdu1075 What Are You Talking About

Hdu1298 T9

Hdu1800 Flying to the Mars

Zoj 1109 Language of FatMouse

Dictionary tree example: hdu1251 statistical difficulties

This is a very classic dictionary tree problem, but here you must pay attention to the choice of the compiler when submitting code. If you select g ++, it must be a burst of memory, therefore, you must select C ++ for submission. Why? Because g ++ is a 64-bit compiler, c ++ is a 32-bit compiler. That is to say, with the same number of pointers, g ++ is twice that of c ++. Of course, this issue is limited to hdu. Previously I handed in with g ++, and then the problem came, and the memory went viral. Finally, we know that there is another thing, and then we can use the AC. It's a waste of time for me to correct my mistakes. It's just a tear.

 

# Include
 
  
# Include
  
   
# Include
   
    
Typedef struct Node {Node * child [26]; int cnt;} Trie; Trie * root; // define the root Node Trie * addnode () {// apply for a new Node space and initialize Trie * u = (Trie *) malloc (sizeof (Trie); for (int I = 0; I <26; I ++) {u-> child [I] = NULL;} return u;} void remove_tree (Trie * u) {// release the memory. Be sure to pay attention !!! If (u = NULL) return; for (int I = 0; I <26; I ++) remove_tree (u-> child [I]); free (u ); u = NULL; return;} void Insert (char * s) {// Insert a string and count Trie * u = root; for (int I = 0; I <strlen (s); I ++) {if (u-> child [s [I]-'a'] = NULL) {Trie * newnode = addnode (); u-> child [s [I]-'a'] = newnode; u-> cnt = 1 ;} else {u = u-> child [s [I]-'a']; u-> cnt = u-> cnt + 1 ;}}} int find (char * s) {// search string Trie * u = ro Ot; for (int I = 0; I <strlen (s); I ++) {if (u-> child [s [I]-'a']! = NULL) u = u-> child [s [I]-'a']; else return 0;} return u-> cnt;} int main () {int n; char s [15]; root = addnode (); // create the root node root-> cnt = 0; // initialize while (gets (s), strcmp (s, "") Insert (s); // tree construction while (scanf ("% s", s )! = EOF) {n = find (s); printf ("% d \ n", n);} remove_tree (root); // release memory return 0 ;}
   
  
 

 

 

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.