Trie Tree--Detailed

Source: Internet
Author: User

L Trie principle

The core idea of Trie is space change time. Use the common prefix of a string to reduce the cost of query time to achieve the purpose of increasing efficiency.

L- Trie Property

Many people say that trie root node does not contain any character information, I am accustomed to the trie root node is contained information, and that this is also convenient, the following is about its nature (based on the simple trie tree discussed in this article)

1. The number of characters determines the degree of each node, i.e. the branch array (space-time idea)

2. The subscript of the Branch Array represents the relative position of the word typeface for a

3. Use the tag method to determine whether it is a string.

4. Insert, find the complexity of all O (len), Len is the length of the string

L Trie .

, the trie tree contains the ABC, D, DA, dda four strings, if the string is marked at the end of the node. Branch branch with no subsequent characters points to null

L examples of Advantages of Trie Trie

A word with an average length of 10, known as N lowercase letters, to determine if there is a string that is prefixed to another string. Here are 3 ways to compare:

1. easiest to think about: searching back from the string set to see if each string is a prefix to a string in a string set, and the complexity is O (n^2).

2. Use hash: We use hash to save all the prefix substrings of all strings. The complexity of establishing a hash of a substring is O (n*len). The complexity of the query is O (n) * O (1) = O (n).

3. use Trie: Because when a query such as String ABC is a prefix of a string, it is obvious to b,c,d .... You don't have to look for a string that doesn't start with a. So the complexity of establishing Trie is O (N*len), and the Establishment + query in Trie can be executed at the same time, the process of establishment can become the process of query, hash cannot achieve this function. So the total complexity is O (N*len), and the complexity of the actual query is just O (len).

Explain why the hash can not be set up and query execution at the same time, for example, there are strings: 911,911456 input, if you want to execute the establishment and query at the same time, the process is query 911, no, then deposited 9, 91, 911, query 911456, did not then deposit 9114, 91145, 911456, and the program does not have memory function, do not know that 911 in the input data has occurred. Therefore, the hash must first be stored in all substrings, and then for loop query.

and Trie tree can, deposit 911, has recorded 911 for the occurrence of the string, in the process of depositing 911456 can be found and output the answer; Upside down also can be, first deposit 911456, at the time of deposit 911, when the pointer points to the last 1 o'clock, the program will find that the 1 already exists, Description 911 must be a string prefix, the idea is I do pku on the 3630 found, see the article supporting the "introductory exercise."

l Trie Simple reality (INSERT, inquire )

1  2#include <iostream>3  using namespacestd;4  5  Const intBranchnum = -;//declaring constants6  inti;7  structTrie_node8  {9     BOOLISSTR;//records whether a string is formed here. TenTrie_node *next[branchnum];//pointer to each subtree, subscript 0-25 for 26 characters OneTrie_node (): Isstr (false) A     { -memset (Next,null,sizeof(next)); -     } the }; -  - classTrie - { +  Public: - Trie (); +     voidInsertConst Char*word); A     BOOLSearchChar*word);  at     voidDeletetrie (Trie_node *root); - Private: -trie_node*Root; - }; -  - Trie::trie () in { -Root =NewTrie_node (); to } +  - voidTrie::insert (Const Char*word) the { *Trie_node *location =Root; $      while(*word)Panax Notoginseng     { -         if(location->next[*word-'a'] = = NULL)//does not exist then establishes the         { +Trie_node *tmp =NewTrie_node (); Alocation->next[*word-'a'] =tmp; the         }     +Location = location->next[*word-'a'];//Each insertion step is equivalent to a new string passing through, and the pointer moves down -word++; $     } $Location->isstr =true;//reach the tail, mark a string - } -  the BOOLTrie::search (Char*word) - {WuyiTrie_node *location =Root; the      while(*word &&Location ) -     { WuLocation = location->next[*word-'a']; -word++; About     } $     return(Location!=null && location->isstr); - } -  - voidTrie::d Eletetrie (Trie_node *root) A { +      for(i =0; i < Branchnum; i++) the     { -         if(Root->next[i]! =NULL) $         { theDeletetrie (root->next[i]); the         } the     } the     DeleteRoot; - } in  the voidMain ()//Simple Test the { About Trie t; theT.insert ("a");  theT.insert ("Abandon"); the     Char* C ="abandoned"; + T.insert (c); -T.insert ("abashed"); the     if(T.search ("abashed"))Bayiprintf"true\n"); the}
View Code

Getting started exercise: PKU POJ 3630 Problem Solving report

                                                                                                                                                                             Turn from:CHerish_yimi (http://www.cnblogs.com/cherish_yimi/)

Trie Tree--Detailed

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.