"Classic Data Structure" Trie

Source: Internet
Author: User

In computer science,Trie, also known as a prefix tree or a dictionary tree , is a kind of tree that holds associative arrays, where keys are usually strings. Unlike a two-fork lookup tree, a key is not stored directly in a node, but is determined by the position of the node in the tree. All descendants of a node have the same prefix, that is, the string corresponding to the node, and the root node corresponds to an empty string. In general, not all nodes have corresponding values, only the leaf node and some internal nodes corresponding to the key has the relevant value.

Here are a few of the blogs that are explained in detail:

1. http://blog.csdn.net/hackbuteer1/article/details/7964147

2. http://blog.csdn.net/v_july_v/article/details/6897097

3. http://blog.csdn.net/luxiaoxun/article/details/7937589

4. "Benevolent Programmer" P276

Scope of Use:

1. Word frequency statistics 2. Prefix match 3. Match all words

1#include <iostream>2#include <string>3 using namespacestd;4 5 classtrie{6  Public:7 Trie ();8~Trie ();9     voidInsert (Const string&s);Ten     BOOLSearch (Const string&s)Const; One Private: A     structtrienode{ -             intCount//number of occurrences of a word -Trienode *next[ -];  the             BOOLexist; -  -Trienode (): Count (0), exist (false) { -Memset (Next, NULL,sizeof(next)); +             } -     }; +  ATrienode *root_; at  -     voidMakeempty (Trienode *root); - }; -  - Trie::trie () { -Root_ =NewTrienode (); in } -  totrie::~Trie () { + Makeempty (root_); - } the  * voidTrie::insert (Const string&s) { $     intLen =s.size ();Panax NotoginsengTrienode *position =root_; -      for(inti =0; i < Len; ++i) {//does not exist establish a node the         if(Position->next[s[i]-'a'] ==NULL) { +Position->next[s[i]-'a'] =NewTrienode (); A         } thePosition = Position->next[s[i]-'a'];//no one step, equivalent to a new string passing, the pointer to move down +     } -  $position->count++; $Position->exist =true; - } -  the BOOLTrie::search (Const string&s)Const { -     if(Root_ = = NULL)return false;Wuyi  the     intLen =s.size (); -Trienode *location =root_; Wu      for(inti =0; i < Len; ++i) { -         if(Location->next[s[i]-'a'] ==NULL) { About             return false; $         }  -Location = Location->next[s[i]-'a']; -     } -  A     returnLocation->exist; + } the  - voidTrie::makeempty (Trienode *root) { $      for(inti =0; I < -; ++i) { the         if(Root->next[i]! =NULL) theMakeempty (root->next[i]); the     } the  - Delete root; inRoot =NULL; the}

"Classic Data Structure" Trie

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.