0
/**
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <cs50.h>
#include <string.h>
#include "dictionary.h"
//define a trie node
typedef struct _node{
    bool isword;
    struct _node *lett[27];
}
tnode;
tnode *head = NULL;
unsigned int word = 0;
int hcode(char character)
{
    if (isalpha(character)){
        return tolower(character) - 97;
}
    else if(character + 0==39){
        return 26;
    }
        return false;
    }
char dehash(int character)
{
    if (character<26 && character >= 0){
        return character + 97;
}
    else if(character ==26){
        return 39;
    }
        return false;
    }
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    // TODO
    return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
 char str[45];
bool load(const char *dictionary)
{
    FILE *dc = fopen(dictionary, "r");
    if(dc == NULL){
        return false;
    }
    head = malloc(sizeof(tnode));
    tnode* cursor = head;

    while (fscanf(dc, "%s", str) != EOF){
        word++;
        cursor = head;
        if(head == NULL){
            return false;
        }
        for(int b = 0;b<strlen(str);b++){
            printf("0");
            if(cursor->lett[hcode(b)]==NULL){
                tnode* newnode = malloc(sizeof(tnode));
                if(newnode == NULL){
                    return false;
                }
                *newnode = (tnode) {false,{NULL}};
                cursor->lett[hcode(b)] = newnode;

            }
            printf("1");
            if(b == strlen(str)-1){
                cursor->lett[hcode(b)]->isword = true;
            }
            cursor=cursor->lett[hcode(b)];
        }
        printf("2\n");
    }    
    fclose(dc);
    // TODO
    return true;

}

I've tried debugging and figuring out the problem. I'm trying to implement load. In my trie, I can only go in one node deep from the header node without suffering a seg fault. I don't know why the node in the array is null. Could I have some help?

1 Answer 1

1

No sure where seg fault comes from, but it basically doesn't matter as you do something completely bizarre.

Let's look what you are doing:

  1. read the whole word from dictionary. Let it be "foo".

  2. calculate its length strlen(str) ==3.

  3. iterate from b == 0 to 2

  4. calculate hcode(b). hcode(b) == 0 for b==0,1,2 (as well as for 3,4,5......)

  5. cursor = cursor->lett[hcode(b)] that is cursor = cursor->lett[0]

and it is the same for any word, "foo", "baz", "age" whatever.

You should take not the whole word, but each word letter by letter, check if cursor -> lett[..] is NULL, if yes first create a new tnode and go there, if not just go there.

1
  • Thank you so much! That mistake fixed everything!
    – ssk4988
    Commented Aug 3, 2017 at 22:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .