Trie

程序修改自 http://zh.wikipedia.org/wiki/Trie

 

1. #include <iostream> #include <string> #include <vector> using namespace std; extern vector<string> tokenize(const string& src, string tok, bool trim = false, string null_subst = ""); const int TREE_WIDTH = 256; const int WORDLENMAX = 128; class TrieNode { public: int count; TrieNode *next[TREE_WIDTH]; TrieNode(int cnt = 0) : count(cnt) { for (int i = 0; i < TREE_WIDTH; i++) { next[i] = NULL; } } }; class Trie { public: Trie() { root = new TrieNode(); } int insert(const char* word); void travel() { travel(root); } private: void travel(TrieNode *p); TrieNode* root; }; const char *spaces = " /r/n/./"/'(),"; int Trie::insert(const char *word) { int i; TrieNode *curr, *newnode; if (word[0] == '/0') { return 0; } curr = root; for (i = 0;; ++i) { if (curr->next[ word[i] ] == NULL) { newnode = new TrieNode(); curr->next[ word[i] ] = newnode; } if (word[i] == '/0') { break; } curr = curr->next[ word[i] ]; } curr->count++; return 0; } static void printword(const char *str, int n) { cout << str << "/t"<< n << endl; } void Trie::travel(TrieNode *p) { static char worddump[WORDLENMAX+1]; static int pos=0; int i; if (p == NULL) { return; } if (p->count) { worddump[pos]='/0'; printword(worddump, p->count); } for (i=0; i<TREE_WIDTH; ++i) { worddump[pos++]=i; travel(p->next[i]); pos--; } return; } int main(void) { Trie trie; cout << "please input a text paragraph :"<< endl; string text; // no need to use cin >> noskipws here getline(cin, text); vector<string> v = tokenize(text, spaces, true); for (unsigned int i = 0; i < v.size(); i++) { trie.insert(v[i].c_str()); } cout << "sorting the words with occurring times "<< endl; trie.travel(); return 0; }

 

2. 字符串分词的代码 From: http://www.diybl.com/course/3_program/c++/cppsl/2008831/139194.html

#include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; typedef basic_string<char>::size_type S_T; static const S_T npos = -1; ////trim指示是否保留空串,默认为保留。tok可以为任意多个字符 vector<string> tokenize(const string& src, string tok, bool trim = false, string null_subst = "") { if (src.empty() || tok.empty() ) throw "tokenize: empty string/0"; vector<string> v; S_T pre_index = 0, index = 0, len = 0; while ( (index = src.find_first_of(tok, pre_index)) != string::npos ) { if ( (len = index - pre_index) != 0) { v.push_back(src.substr(pre_index, len)); } else if (trim == false) { v.push_back(null_subst); } pre_index = index + 1; } string endstr = src.substr(pre_index); if (trim == false) { v.push_back(endstr.empty() ? null_subst : endstr ); } else if ( !endstr.empty() ) { v.push_back(endstr); } return v; } //delimit为一个字符,严格分割 vector<string> split(const string& src, string delimit, string null_subst="") { if (src.empty() || delimit.empty() ) throw "split:empty string/0"; vector<string> v; S_T deli_len = delimit.size(); long index = npos, last_search_position = 0; while ( (index = src.find(delimit, last_search_position)) != npos ) { if (index == last_search_position) v.push_back(null_subst); else v.push_back(src.substr(last_search_position, index - last_search_position) ); last_search_position = index + deli_len; } string last_one = src.substr(last_search_position); v.push_back(last_one.empty() ? null_subst : last_one); return v; } //int main(int argc, char* argv[]) { // string src = ",ab,cde;,,fg,,"; // string tok = ",;"; // vector<string> v1 = tokenize(src, tok , true); // vector<string> v2 = tokenize(src, tok , false, "<null>"); // cout<<"-------------v1:"<<endl; // for (int i=0; i<v1.size(); i++) { // cout<<v1[i].c_str()<<endl; // } // cout<<"-------------v2:"<<endl; // for (int j=0; j<v2.size(); j++) { // cout<<v2[j].c_str()<<endl; // } // try { // // string s = "1;2;3;4"; // string del = ";";//"###"; // vector<string> v3 = split(s, del, "<null>"); // cout<<"-------------v3:"<<endl; // for(int k=0; k<v3.size();k++) // { // cout<<v3[k].c_str()<<endl; // } // } // catch (char *s) { // cout<<s<<endl; // } // return 0; //}

 

3. 测试结果如下:

please input a text paragraph : In computer science, a trie, or prefix tree, is an ordered tree data structure that is used to store an associative array where the keys are usually strings. Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree shows what key it is associated with. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string. Values are normally not associated with every node, only with leaves and some inner nodes that correspond to keys of interest. sorting the words with occurring times All 1 In 1 Unlike 1 Values 1 a 4 an 2 and 2 are 2 array 1 associated 5 associative 1 binary 1 common 1 computer 1 correspond 1 data 1 descendants 1 empty 1 every 1 have 1 in 2 inner 1 instead 1 interest 1 is 4 it 1 its 1 key 2 keys 2 leaves 1 no 1 node 4 node; 1 nodes 1 normally 1 not 1 of 3 only 1 or 1 ordered 1 position 1 prefix 2 root 1 science 1 search 1 shows 1 some 1 store 1 stores 1 string 2 strings 1 structure 1 that 4 the 8 to 2 tree 5 trie 1 used 1 usually 1 what 1 where 1 with 6

你可能感兴趣的:(Trie)