Advertisement
Guest User

Untitled

a guest
Jan 17th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.91 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
  6.  
  7. // Alphabet size (# of symbols)
  8. #define ALPHABET_SIZE (26)
  9. // Converts key current character into index
  10. // use only 'a' through 'z' and lower case
  11. #define CHAR_TO_INDEX(C) ((int)C - (int)'A')
  12.  
  13. // trie node
  14. typedef struct trie_node trie_node_t;
  15. struct trie_node
  16. {
  17.     int value;
  18.     trie_node_t *children[ALPHABET_SIZE];
  19. };
  20.  
  21. // trie ADT
  22. typedef struct trie trie_t;
  23. struct trie
  24. {
  25.     trie_node_t *root;
  26.     int count;
  27. };
  28.  
  29. // Returns new trie node (initialized to NULLs)
  30. trie_node_t *getNode(void)
  31. {
  32.     trie_node_t *pNode = NULL;
  33.  
  34.     pNode = (trie_node_t *)malloc(sizeof(trie_node_t));
  35.  
  36.     if( pNode )
  37.     {
  38.         int i;
  39.  
  40.         pNode->value = 0;
  41.  
  42.         for(i = 0; i < ALPHABET_SIZE; i++)
  43.         {
  44.             pNode->children[i] = NULL;
  45.         }
  46.     }
  47.  
  48.     return pNode;
  49. }
  50.  
  51. // Initializes trie (root is dummy node)
  52. void initialize(trie_t *pTrie)
  53. {
  54.     pTrie->root = getNode();
  55.     pTrie->count = 0;
  56. }
  57.  
  58. // If not present, inserts key into trie
  59. // If the key is prefix of trie node, just marks leaf node
  60. void insert(trie_t *pTrie, char key[])
  61. {
  62.     int level;
  63.     int length = strlen(key);
  64.     int index;
  65.     trie_node_t *pCrawl;
  66.  
  67.     pTrie->count++;
  68.     pCrawl = pTrie->root;
  69.  
  70.     for( level = 0; level < length; level++ )
  71.     {
  72.         index = CHAR_TO_INDEX(key[level]);
  73.         if( !pCrawl->children[index] )
  74.         {
  75.             pCrawl->children[index] = getNode();
  76.         }
  77.  
  78.         pCrawl = pCrawl->children[index];
  79.     }
  80.     printf("count%d\n", pTrie->count);
  81.     // mark last node as leaf
  82.     pCrawl->value = pTrie->count;
  83. }
  84.  
  85. // Returns non zero, if key presents in trie
  86. int search(trie_t *pTrie, char key[])
  87. {
  88.     int level;
  89.     int length = strlen(key);
  90.     int index;
  91.     trie_node_t *pCrawl;
  92.  
  93.     pCrawl = pTrie->root;
  94.  
  95.     for( level = 0; level < length; level++ )
  96.     {
  97.         index = CHAR_TO_INDEX(key[level]);
  98.  
  99.         if( !pCrawl->children[index] )
  100.         {
  101.             return 0;
  102.         }
  103.  
  104.         pCrawl = pCrawl->children[index];
  105.     }
  106.  
  107.     return (0 != pCrawl && pCrawl->value);
  108. }
  109.  
  110. // Driver
  111. int main()
  112. {
  113.     // Input keys (use only 'a' through 'z' and lower case)
  114.     char keys[][10] = {"THE", "THE", "THE", "A", "THERE", "ANSWER", "ANY", "BY", "BYE", "THEIR"};
  115.     trie_t trie;
  116.  
  117.     char output[][20] = {"Not present in trie", "Present in trie"};
  118.  
  119.     initialize(&trie);
  120.  
  121.     // Construct trie
  122.     for(int i = 0; i < ARRAY_SIZE(keys); i++)
  123.     {
  124.         insert(&trie, keys[i]);
  125.     }
  126.  
  127.     // Search for different keys
  128.     printf("%s --- %s\n", "THE", output[search(&trie, "THE")] );
  129.     printf("%s --- %s\n", "THESE", output[search(&trie, "THESE")] );
  130.     printf("%s --- %s\n", "THEIR", output[search(&trie, "THEIR")] );
  131.     printf("%s --- %s\n", "THAW", output[search(&trie, "THAW")] );
  132.  
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement