Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4.  
  5. struct node{
  6.     char a;
  7.     bool leaf;
  8.     struct node *child[26];
  9. }*root;
  10.  
  11. struct node* newNode(char c)
  12. {
  13.     struct node *temp = new node();
  14.     temp->a = c;
  15.     temp->leaf = 0;
  16.     for(int i=0;i<26;i++)
  17.         temp->child[i] = NULL;
  18.     return temp;
  19. }
  20.  
  21. void build_trie(string k, int len, struct node* head)
  22. {
  23.     struct node *curr = head;
  24.     for(int i=0;i<len;i++)
  25.     {
  26.  
  27.         if(curr->child[k[i]-'a']==NULL)
  28.             curr->child[k[i]-'a'] = newNode(k[i]);
  29.  
  30.         curr = curr->child[k[i]-'a'];
  31.     }
  32.  
  33.     curr->leaf =1;
  34. }
  35.  
  36. void print_trie(struct node *head, char pr[], int num)
  37. {
  38.     for(int i=0;i<26;i++)
  39.     {
  40.         if(head->child[i]!=NULL)
  41.         {
  42.             //cout<<"Data: "<<head->child[i]->a;
  43.             pr[num] = head->child[i]->a;
  44.             num++;
  45.             print_trie(head->child[i],pr,num);
  46.  
  47.             if(head->child[i]->leaf == 1)
  48.             {   for(int i=0;i<num;i++)
  49.                     cout<<pr[i]<<" ";
  50.                 cout<<"\n";
  51.             }
  52.             num--; 
  53.  
  54.         }
  55.     }
  56. }
  57.  
  58. bool search_trie(struct node *head, string k)
  59. {
  60.     int len = k.size();
  61.     bool b = 1;
  62.     int start =0 ;
  63.     while(b)
  64.     {
  65.         if(head->child[k[start]-'a']!=NULL)
  66.         {  
  67.             if(start==len-1)
  68.             {
  69.                 if(head->child[k[start]-'a']->leaf != 1)
  70.                     b=0;
  71.             }
  72.             head = head->child[k[start]-'a'];
  73.             start++;
  74.         }
  75.         else
  76.             b=0;
  77.         if(start==len)
  78.             break;
  79.     }
  80.     return b;
  81. }
  82.  
  83. int main()
  84. {
  85.  
  86.     //string arr[4] = {'neha','gauti','kesp','keshav'};
  87.     //string s="keshav";
  88.     char p='0';
  89.     root = newNode(p);
  90.     char pr[100];
  91.     //for(int i=0;i<4;i++)
  92.     //{
  93.         //int len= arr[i].size();
  94.         //cout<<len;
  95.         //build_trie(arr[i],len,root);
  96.         build_trie("keshav",6,root);
  97.         build_trie("kesh",4,root);
  98.         build_trie("gauti",5,root);
  99.         build_trie("neha",4,root);
  100.     //}
  101.     print_trie(root,pr,0);
  102.     if(search_trie(root,"kesh"))
  103.         cout<<"In trie";
  104.     else
  105.         cout<<"Not in trie";
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement