Advertisement
abhinavarora

Implementation of a Trie

Jan 3rd, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.91 KB | None | 0 0
  1. /* Program to Implement the Trie data Structure */
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5. #define MAX 21
  6. struct node
  7. {
  8.        int val;
  9.        struct node *children[26];
  10.        };
  11. struct trie_t{
  12.        struct node *root;
  13.        int count;
  14.        };
  15.        
  16. struct node *getnode();
  17. struct trie_t* initialise();
  18. void insert(struct trie_t *,char []);
  19. void display(struct trie_t*);
  20. void displayrec(struct node *,char [],int);
  21. void printpath(char [],int);
  22. int search(struct trie_t *,char []);
  23. void del(struct trie_t *,char []);
  24. int delhelp(struct node *,char [],int,int);
  25. main()
  26. {
  27.       int i;
  28.       struct trie_t *trie;
  29.       char key[MAX];
  30.       trie=initialise();
  31.       if(!trie)
  32.       {
  33.                printf("Error");
  34.                exit(0);
  35.                }
  36.       while(1)
  37.       {
  38.               printf("Select an Option :\n");
  39.               printf("1. Insert\n2. Search\n3. Display All Keys\n4. Delete Key\n5. Exit\n");
  40.               scanf("%d",&i);
  41.               putchar('\n');
  42.               switch(i)
  43.               {
  44.                        case 1:
  45.                             printf("Enter the word to be added : ");
  46.                             scanf("%s",key);
  47.                             insert(trie,key);
  48.                             printf("\n%s successfully inserted",key);
  49.                             break;
  50.                        case 2:
  51.                             printf("Enter the word to be searched : ");
  52.                             scanf("%s",key);
  53.                             if(search(trie,key))
  54.                                                 printf("\n%s found in the Trie",key);
  55.                             else
  56.                                                 printf("\n%s not found in the Trie",key);  
  57.                             break;                
  58.                        case 3:
  59.                             if(trie)
  60.                                     display(trie);
  61.                             break;
  62.                        case 4:
  63.                             printf("Enter the entry to be deleted from the Trie : ");
  64.                             scanf("%s",key);
  65.                             del(trie,key);
  66.                             break;
  67.                        case 5:
  68.                             exit(0);
  69.                        }
  70.               getch();
  71.               system("cls");
  72.               }
  73.       }
  74. struct node *getnode()
  75. {
  76.        struct node *tnode;
  77.        int i;
  78.        tnode=(struct node *)malloc(sizeof(struct node));
  79.        tnode->val=0;
  80.        for(i=0;i<26;i++)
  81.                         tnode->children[i]=NULL;
  82.        return tnode;
  83.        }
  84. struct trie_t *initialise()
  85. {
  86.        struct trie_t* t;
  87.        t=(struct trie_t *)malloc(sizeof(struct trie_t));
  88.        t->root=getnode();
  89.        t->count=0;
  90.        return t;
  91.        }
  92.  
  93. void insert(struct trie_t *trie,char key[])
  94. {
  95.      int len=strlen(key),i,index;
  96.      struct node *crawl=trie->root;
  97.      if(!trie)
  98.               return;
  99.      trie->count++;
  100.      for(i=0;i<len;i++)
  101.      {
  102.                        index=key[i]-'a';
  103.                        if(crawl->children[index]==NULL)
  104.                                                    crawl->children[index]=getnode();
  105.                        crawl=crawl->children[index];
  106.                        }
  107.      crawl->val=trie->count;
  108.      }
  109. void display(struct trie_t *trie)
  110. {
  111.      char path[MAX];
  112.      int i;
  113.      if(trie->count==0)
  114.                        printf("The Trie is Empty");
  115.      else
  116.      {
  117.                        printf("The Trie contains %d elements :\n",trie->count);
  118.                        displayrec(trie->root,path,0);
  119.                        }
  120.      }
  121.  
  122. void displayrec(struct node *root,char path[],int len)
  123. {
  124.      int i;
  125.      if(root==NULL)
  126.                    return;
  127.      if(root->val)
  128.                   printpath(path,len);
  129.      for(i=0;i<26;i++)
  130.                       if(root->children[i])
  131.                       {
  132.                                            path[len]='a'+i;
  133.                                            displayrec(root->children[i],path,len+1);
  134.                                            }
  135.      }
  136.  
  137. void printpath(char path[],int len)
  138. {
  139.      int i;
  140.      for(i=0;i<len;i++)
  141.                        putchar(path[i]);
  142.      putchar('\n');
  143.      }
  144.  
  145. int search(struct trie_t *trie,char key[])
  146. {
  147.     int i,len,index;
  148.     struct node *crawl=trie->root;
  149.     len=strlen(key);
  150.     for(i=0;i<len;i++)
  151.     {
  152.                       index=key[i]-'a';
  153.                       if(crawl->children[index]==NULL)
  154.                                                   return 0;
  155.                       crawl=crawl->children[index];
  156.                       }
  157.     return crawl&&crawl->val;
  158.     }
  159. void del(struct trie_t *trie,char key[])
  160. {
  161.     int len,ret;
  162.     len=strlen(key);
  163.     if(len)
  164.          ret=delhelp(trie->root,key,0,len);  
  165.     if(ret==0 || ret==1)
  166.     {
  167.               trie->count--;
  168.               printf("%s successfully deleted from Trie",key);
  169.               }
  170.     }
  171.    
  172. int delhelp(struct node *root,char key[],int lev,int len)
  173. {
  174.     int index,ret,temp=0,check,i;
  175.     if(root==NULL)
  176.     {
  177.                   printf("%s not found in the Trie",key);
  178.                   return 2;  // This means that the nodes above are not to be deleted and key not found
  179.                   }
  180.     else if(lev==len)
  181.     {
  182.          if(root->val)
  183.          {
  184.                       root->val=0;
  185.                       for(i=0,check=1;i<26;i++) // check==1 means we can delete  check==0 means we can not delete
  186.                                  if(root->children[i])
  187.                                  {
  188.                                                       check=0;
  189.                                                       break;
  190.                                                       }
  191.                       if(check)
  192.                       {
  193.                                free(root);
  194.                                return 0;
  195.                                }
  196.                       else
  197.                           return 1;
  198.                       }
  199.          else
  200.              return 2;
  201.          }
  202.     else
  203.     {
  204.         index=key[lev]-'a';
  205.         ret=delhelp(root->children[index],key,lev+1,len);
  206.         if(ret==0)
  207.                   root->children[index]=NULL; // Node below it deleted in the recursive call
  208.         for(i=0,check=1;i<26;i++) // check==1 means we can delete  check==0 means we can not delete
  209.                                  if(root->children[i])
  210.                                  {
  211.                                                       check=0;
  212.                                                       break;
  213.                                                       }
  214.         if((!(temp=root->val || ret)) && check)
  215.         {                  
  216.                             free(root);
  217.                             return 0;
  218.                             }
  219.         else
  220.              return (ret?ret:1);
  221.         }
  222.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement