Advertisement
nasarouf

TRIE

Dec 21st, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1.  
  2. //call global function initialize() before using this class.
  3. //root.add(char *t, flag, len_of_t);  -- here flag is some unique id, count is updated based if a different flag is supplied.
  4. //insert cod eto trav functions
  5.  
  6.  
  7. #define TRIE_MAX 150000
  8. #define TRIE_CHARS 26
  9. #define TRIE_OFF 'a'
  10.  
  11.  
  12. char trie_buf[TRIE_MAX]; // this is where all the 'strings' are saved
  13. int trie_nbuf;
  14.  
  15.  
  16. class Trie{
  17.    
  18. public:
  19.    
  20.     Trie *ch[TRIE_CHARS],*parent;
  21.     char* s;
  22.     int len,h,count,substrcount,lastflag;
  23.    
  24.     static void initialize(){
  25.         trie_nbuf=0;
  26.     }
  27.  
  28.  
  29.     void clear();
  30.    
  31.     void add(char*, int, int);
  32.    
  33.     //not tested
  34.     Trie* find(char* t, int tlen=-1){
  35.         if (tlen==-1) tlen=strlen(t);      
  36.         if (len==tlen && strncmp(s,t,len)==0) return this;
  37.         if (tlen<len) return NULL;
  38.         if (strncmp(s,t,len)!=0) return NULL;
  39.         if (ch[t[len]-TRIE_OFF]==NULL) return NULL;
  40.         return ch[t[len]-TRIE_OFF]->find(t+len,tlen-len);
  41.     }
  42.    
  43.     /*
  44.     string getstring(){
  45.         if (parent==NULL) return s;
  46.         return parent->getstring()+s;
  47.     }
  48.     */
  49.  
  50.  
  51.     void prints(){     for (int i=0; i<len; i++) cout<<s[i]; }
  52.  
  53.  
  54.     void printstring(){
  55.         if (parent!=NULL) parent->printstring();
  56.         prints();
  57.     }
  58.    
  59.     void trav();
  60.  
  61.  
  62.     void trav2();
  63.    
  64.     void print(int lev=0){
  65.         cout<<string(lev,'.');
  66.         prints();
  67.         cout<<"(@"<<h<<",*"<<count<<",[]"<<substrcount<<")"<<endl;
  68.         for (int i=0; i<TRIE_CHARS; i++) if (ch[i]!=NULL) ch[i]->print(lev+1);
  69.     }
  70.    
  71. }nodes[TRIE_MAX],root;
  72.  
  73.  
  74. int nodec;
  75.  
  76.  
  77. void Trie::clear(){
  78.     for (int i=0; i<TRIE_CHARS; i++) ch[i]=NULL;
  79.     h=substrcount=count=len=lastflag=0;
  80.     s=trie_buf;
  81.     parent=NULL;
  82.     //  nodec=0;
  83. }
  84.  
  85.  
  86. Trie* newTrie(){
  87.     nodes[nodec].clear();
  88.     return &nodes[nodec++];
  89. }
  90.  
  91.  
  92. void Trie::add(char* t, int flag, int tlen=-1){
  93.     if (tlen==-1) tlen=strlen(t);
  94.     int i;
  95.     //cout<<"[add] ";    prints(); cout<<","<<t<<endl;
  96.     for (i=0; i<len && i<tlen; i++)
  97.         if (s[i]!=t[i]) break;
  98.     if (i==len && i==tlen){
  99.         count++;
  100.         return;
  101.     }
  102.     if (i<len){
  103.         //create a new node to hold the bottom part
  104.         Trie* node=newTrie();
  105.         node->parent=this;
  106.         node->s=s+i;
  107.         node->len=len-i;
  108.         node->count=count;
  109.         node->substrcount=substrcount;
  110.         node->lastflag=lastflag;
  111.         node->h=h+i;
  112.         for (int j=0; j<TRIE_CHARS; j++){
  113.             node->ch[j]=ch[j];
  114.             if (ch[j]) ch[j]->parent=node;
  115.             ch[j]=NULL;
  116.         }
  117.         //keep the top part in 'this'
  118.         ch[s[i]-TRIE_OFF]=node;
  119.         len=i;
  120.         if (i==tlen) count++;
  121.     }
  122.     if (i<tlen){
  123.         if (ch[t[i]-TRIE_OFF]==NULL){
  124.             Trie* node=newTrie();
  125.             node->parent=this;
  126.             strcpy(trie_buf+trie_nbuf,t+i);
  127.             node->s=trie_buf+trie_nbuf;
  128.             node->len=tlen-i;
  129.             trie_nbuf+=tlen-i;
  130.             node->count=1;
  131.             node->substrcount=1;
  132.             node->lastflag=flag;
  133.             node->h=h+i;
  134.             ch[t[i]-TRIE_OFF]=node;
  135.         }
  136.         else ch[t[i]-TRIE_OFF]->add(t+i,flag,tlen-i); //sometimes when i==s.length()
  137.     }
  138.     if (lastflag!=flag) substrcount++;
  139.     lastflag=flag;
  140. }
  141.  
  142.  
  143. int n,maxlen;
  144.  
  145.  
  146. void Trie::trav(){
  147.     /* insert code */
  148.     if (substrcount*2>n && h+(signed)len>maxlen)
  149.         maxlen=h+(signed)len;
  150.     /* end insert */
  151.     for (int i=0; i<TRIE_CHARS; i++){
  152.         if (ch[i]!=NULL)
  153.             ch[i]->trav();
  154.     }
  155. }
  156.  
  157.  
  158. void Trie::trav2(){
  159.     /* insert code */
  160.     if (substrcount*2>n && h+(signed)len==maxlen){
  161.         printstring();
  162.         cout<<endl;
  163.     }
  164.     /* end insert */
  165.     for (int i=0; i<TRIE_CHARS; i++){
  166.         if (ch[i]!=NULL)
  167.             ch[i]->trav2();
  168.     }
  169. }
  170.  
  171.  
  172. void initialize(){
  173.     Trie::initialize();
  174.     root.clear();
  175.     nodec=0;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement