Dictionary tree Template

Source: Internet
Author: User
My dictionary tree Template


# Define Maxnum 26 // Define dictionary Tree Structure Typedef Struct Trie { Bool Flag; // Is it a word from the root? Trie * Next [maxnum];} trie; // Declare a root Trie * Root; // Initialize the root Void Init () {Root = (Trie *) malloc ( Sizeof (Trie); root -> Flag = False ; For ( Int I = 0 ; I <maxnum; I ++ ) Root -> Next [I] = NULL ;} // Insert words into the dictionary tree Void Insert ( Char *Word) {Trie * TEM = Root; While (* Word! = ' \ 0 ' ){ If (TEM-> next [* word- ' A ' ] = Null) {Trie * Cur = (trie *) malloc ( Sizeof (Trie )); For ( Int I = 0 ; I <maxnum; I ++ ) Cur -> Next [I] = NULL; cur -> Flag = False ; TEM -> Next [* word- ' A ' ] = Cur;} TEM = Tem-> next [* word- ' A ' ]; Word ++ ;} TEM -> Flag = True ;} // Query a word Bool Search ( Char * Word) {Trie * TEM = Root; For ( Int I = 0 ; Word [I]! =' \ 0 ' ; I ++ ){ If (TEM = NULL | tem-> next [word [I]- ' A ' ] = Null) Return False ; TEM = Tem-> next [word [I]- ' A ' ];} Return TEM-> Flag ;} // Release the dictionary Tree Memory Operation.ProgramAutomatically jumps out, so no memory release function is written here Void Del (trie * Cur ){ For ( Int I = 0 ; I <maxnum; I ++ ){ If (Cur-> next [I]! = Null) del (cur ->Next [I]);} Free (cur );}

For details about the dictionary tree, refer to Baidu Baike's interpretation of the dictionary tree. The template can be used to solve this type of general problems with slight changes.

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.