pset5/speller/dictionary_trie/dictionaryTRIE.c
bool load(const char *dictionary)
{
    
    // create the first element of the trie
    root = malloc(sizeof(node));
    if (root == NULL)
    {
        fprintf(stderr, "Not enough memory.\n");
        return false;
    }
    
    // initialize its fields
    root -> is_word = false;
    for (uint8_t i = 0; i < 27; i++)
    {
        root -> children[i] = NULL;
    }

    // single word buffer
    char *w;
    w = malloc( (LENGTH + 1)* sizeof(char));    // LENGTH + '\0'
    if (w == NULL)
    {
        free(root);
        fprintf(stderr, "Not enough memory.\n");
        return false;
    }
    
    // open dictionary file 
    FILE *dfile = fopen(dictionary, "r");
    if (dfile == NULL)
    {
        free(w);
        free(root);
        return false;
    }
    
    // temporary variables
    node *tmp, *tmp2;
    
    // read the dictionary word by word and save each word's hash in the table
    while ( fscanf(dfile, "%s\n", w) != EOF )
    {
        // count the words
        words++;
        
        // buffer variables
        uint8_t len = strlen(w);
        uint8_t c;
        tmp = root;
        
        // create a trie path for the current word
        for (uint8_t i = 0; i < len; i++)
        {
            c = shift(w[i]);

            // position doesnt exists - create it
            if (tmp -> children[c] == NULL)
            {

                // create new node
                tmp2 = malloc(sizeof(node));
                if (tmp2 == NULL)
                {
                    unload();
                    free(w);
                    fprintf(stderr, "Not enough memory.\n");
                    return false;
                }
                
                // initialize its fields
                for (uint8_t j = 0; j < 27; j++)
                {
                    tmp2 -> children[j] = NULL;
                }
                tmp -> children[c] = tmp2;
            }
            
            // point to the next child
            tmp = tmp -> children[c];
        }
        
        // set the boolean flag at the end
        tmp -> is_word = true;
    }

    free(w);
    fclose(dfile);
    
    return true;   
}
/**
 * Loads dictionary into memory. Returns true if successful else false.
 * 
 */
Loads dictionary into memory. Returns true if successful else false.
Loads dictionary into memory . Returns true if successful else false .
bool load(const char *dictionary)
{
    root = malloc(sizeof(node));
    if (root == NULL)
    {
        fprintf(stderr, "Not enough memory.\n");
        return false;
    }
    root -> is_word = false;
    for (uint8_t i = 0; i < 27; i++)
    {
        root -> children[i] = NULL;
    }
    char *w;
    w = malloc( (LENGTH + 1)* sizeof(char));    
    if (w == NULL)
    {
        free(root);
        fprintf(stderr, "Not enough memory.\n");
        return false;
    }
    FILE *dfile = fopen(dictionary, "r");
    if (dfile == NULL)
    {
        free(w);
        free(root);
        return false;
    }
    node *tmp, *tmp2;
    while ( fscanf(dfile, "%s\n", w) != EOF )
    {
        words++;
        uint8_t len = strlen(w);
        uint8_t c;
        tmp = root;
        for (uint8_t i = 0; i < len; i++)
        {
            c = shift(w[i]);
            if (tmp -> children[c] == NULL)
            {
                tmp2 = malloc(sizeof(node));
                if (tmp2 == NULL)
                {
                    unload();
                    free(w);
                    fprintf(stderr, "Not enough memory.\n");
                    return false;
                }
                for (uint8_t j = 0; j < 27; j++)
                {
                    tmp2 -> children[j] = NULL;
                }
                tmp -> children[c] = tmp2;
            }
            tmp = tmp -> children[c];
        }
        tmp -> is_word = true;
    }
    free(w);
    fclose(dfile);
    return true;   
}
bool load ( const char * dictionary ) { root = malloc ( sizeof ( node ) ) ; if ( root == NULL ) { fprintf ( stderr , " \n " ) ; return false ; } root -> is_word = false ; for ( uint8_t i = 0 ; i < 27 ; i ++ ) { root -> children [ i ] = NULL ; } char * w ; w = malloc ( ( LENGTH + 1 ) * sizeof ( char ) ) ; if ( w == NULL ) { free ( root ) ; fprintf ( stderr , " \n " ) ; return false ; } FILE * dfile = fopen ( dictionary , " " ) ; if ( dfile == NULL ) { free ( w ) ; free ( root ) ; return false ; } node * tmp , * tmp2 ; while ( fscanf ( dfile , " \n " , w ) != EOF ) { words ++ ; uint8_t len = strlen ( w ) ; uint8_t c ; tmp = root ; for ( uint8_t i = 0 ; i < len ; i ++ ) { c = shift ( w [ i ] ) ; if ( tmp -> children [ c ] == NULL ) { tmp2 = malloc ( sizeof ( node ) ) ; if ( tmp2 == NULL ) { unload ( ) ; free ( w ) ; fprintf ( stderr , " \n " ) ; return false ; } for ( uint8_t j = 0 ; j < 27 ; j ++ ) { tmp2 -> children [ j ] = NULL ; } tmp -> children [ c ] = tmp2 ; } tmp = tmp -> children [ c ] ; } tmp -> is_word = true ; } free ( w ) ; fclose ( dfile ) ; return true ; }
Loads dictionary into memory.
Loads dictionary into memory .
// create the first element of the trie // initialize its fields // single word buffer // LENGTH + '\0' // open dictionary file  // temporary variables // read the dictionary word by word and save each word's hash in the table // count the words // buffer variables // create a trie path for the current word // position doesnt exists - create it // create new node // initialize its fields // point to the next child // set the boolean flag at the end