Advertisement
Broatlas

Part_A

Apr 22nd, 2016
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. /* PROGRAM:  tire.c
  2.    AUTHOR:  Robert Taracha
  3.    DATE:     9/03/16
  4.    TOPIC:    trie implementation
  5.    PURPOSE:
  6.    NOTES:
  7.  
  8. */
  9.  
  10. /**************************************************************************/
  11. /* Declare include files
  12.  **************************************************************************/
  13. #include "trie.h"
  14.  
  15. /**************************************************************************/
  16. /* Helper Functions
  17.  * trie_node_t * trie_new(  void );
  18.  *         Allocates memory for a new trie node
  19.  *         Returns NULL if memory allocation was not possible or
  20.  *         a memory address to a trie_node in the heap
  21. /**************************************************************************/
  22. trie_node_t * trie_new(  void ){
  23.  
  24.     trie_node_t * tmp = NULL;
  25.     int i;
  26.  
  27.     if ( ( tmp = ( trie_node_t * ) malloc ( sizeof( trie_node_t ) ) ) == NULL )
  28.         return NULL;
  29.  
  30.     for( i = 0; i < ALPHA_SIZE; i++ ) {
  31.         tmp->child[ i ] = NULL;
  32.         tmp->end = 1;
  33.     }
  34.     return tmp;
  35. }
  36.  
  37. /**************************************************************************/
  38. /* Functions functions
  39.  * int trie_size     ( trie_node_t *  root );
  40.  *         Returns the number of words in the trie
  41.  * int trie_contains ( trie_node_t *  root,    char word[ ] );
  42.  *         Returns 1 if a the word is in the trie
  43.  *                 0 otherwise
  44.  * int trie_insert   ( trie_node_t ** rootRef, char word[ ] );
  45.  *         Returns 1 if the word is inserted in the trie
  46.  *                 0 otherwise
  47.  * int trie_remove  ( trie_node_t ** rootRef, char word[ ] );
  48.  *         Returns 1 if the word is removed from the trie
  49.  *                 0 otherwise
  50.  * int trie_destroy  ( trie_node_t ** Tref );
  51.  *         Returns 1 if the trie and all its node are destroyed
  52.  **************************************************************************/
  53. int trie_size     ( trie_node_t *  root ) {
  54.  
  55.  
  56.     return 1;
  57. }
  58.  
  59. /**************************************************************************/
  60. int trie_contains ( trie_node_t *  root,    char word[ ] ){
  61.  
  62.     return 1;
  63. }
  64. /**************************************************************************/
  65. int trie_init(trie_node_t **rootRef){
  66.  
  67.     if (*rootRef == NULL){
  68.         *rootRef = trie_new();
  69.     }
  70. return 1;
  71. }
  72. /**************************************************************************/
  73.  
  74. /**************************************************************************/
  75. int trie_insert   ( trie_node_t ** rootRef, char word[ ] ){
  76.         int i=0, index=0;
  77.         int len;
  78.         trie_node_t *tmp = *rootRef;
  79.     len = strlen(word);
  80.         for(i = 0;i<len;i++){
  81.             word[i] = tolower(word[i]);
  82.         }
  83.  
  84.     if (tmp == NULL) return EXIT_FAILURE;
  85.     for (i=0;i<len;i++) {
  86.             if (word[i] == " \ ") index = 26;
  87.             else if (word[i] >= "A" || word[i] <= "Z") {
  88.                 index = charToInt(toLower(word[i]));
  89.             }
  90.             else return EXIT_FAILURE;
  91.  
  92.             if (!tmp->child[index]) tmp->child[index] = trie_new();
  93.  
  94.             tmp = tmp->child[index];
  95.     }
  96.     tmp->end= "\0";
  97.  
  98.     return 1;
  99. }
  100. /**************************************************************************/
  101. void trie_remove  ( trie_node_t ** rootRef, char word[ ] ){
  102.  
  103.     return 1;
  104. }
  105. /**************************************************************************/
  106. int trie_destroy  ( trie_node_t ** rootRef ){
  107.  
  108.     return 1;
  109. }
  110. /**************************************************************************/
  111. int charToInt(char c){
  112.      return (int)c-(int)'a';
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement