Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* PROGRAM: tire.c
- AUTHOR: Robert Taracha
- DATE: 9/03/16
- TOPIC: trie implementation
- PURPOSE:
- NOTES:
- */
- /**************************************************************************/
- /* Declare include files
- **************************************************************************/
- #include "trie.h"
- /**************************************************************************/
- /* Helper Functions
- * trie_node_t * trie_new( void );
- * Allocates memory for a new trie node
- * Returns NULL if memory allocation was not possible or
- * a memory address to a trie_node in the heap
- /**************************************************************************/
- trie_node_t * trie_new( void ){
- trie_node_t * tmp = NULL;
- int i;
- if ( ( tmp = ( trie_node_t * ) malloc ( sizeof( trie_node_t ) ) ) == NULL )
- return NULL;
- for( i = 0; i < ALPHA_SIZE; i++ ) {
- tmp->child[ i ] = NULL;
- tmp->end = 1;
- }
- return tmp;
- }
- /**************************************************************************/
- /* Functions functions
- * int trie_size ( trie_node_t * root );
- * Returns the number of words in the trie
- * int trie_contains ( trie_node_t * root, char word[ ] );
- * Returns 1 if a the word is in the trie
- * 0 otherwise
- * int trie_insert ( trie_node_t ** rootRef, char word[ ] );
- * Returns 1 if the word is inserted in the trie
- * 0 otherwise
- * int trie_remove ( trie_node_t ** rootRef, char word[ ] );
- * Returns 1 if the word is removed from the trie
- * 0 otherwise
- * int trie_destroy ( trie_node_t ** Tref );
- * Returns 1 if the trie and all its node are destroyed
- **************************************************************************/
- int trie_size ( trie_node_t * root ) {
- return 1;
- }
- /**************************************************************************/
- int trie_contains ( trie_node_t * root, char word[ ] ){
- return 1;
- }
- /**************************************************************************/
- int trie_init(trie_node_t **rootRef){
- if (*rootRef == NULL){
- *rootRef = trie_new();
- }
- return 1;
- }
- /**************************************************************************/
- /**************************************************************************/
- int trie_insert ( trie_node_t ** rootRef, char word[ ] ){
- int i=0, index=0;
- int len;
- trie_node_t *tmp = *rootRef;
- len = strlen(word);
- for(i = 0;i<len;i++){
- word[i] = tolower(word[i]);
- }
- if (tmp == NULL) return EXIT_FAILURE;
- for (i=0;i<len;i++) {
- if (word[i] == " \ ") index = 26;
- else if (word[i] >= "A" || word[i] <= "Z") {
- index = charToInt(toLower(word[i]));
- }
- else return EXIT_FAILURE;
- if (!tmp->child[index]) tmp->child[index] = trie_new();
- tmp = tmp->child[index];
- }
- tmp->end= "\0";
- return 1;
- }
- /**************************************************************************/
- void trie_remove ( trie_node_t ** rootRef, char word[ ] ){
- return 1;
- }
- /**************************************************************************/
- int trie_destroy ( trie_node_t ** rootRef ){
- return 1;
- }
- /**************************************************************************/
- int charToInt(char c){
- return (int)c-(int)'a';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement