如何回到特里结构的顶部

时间:2017-03-28 19:15:06

标签: c trie cs50

我正在研究CS50,并且在我对指针/尝试的理解上有所作为。 我目前正在尝试将多个单词从文件加载到活动内存中。 每当加载循环命中" \ n"时,我想返回到trie的顶部(根)以开始加载一个新单词。 我不知道该怎么做。 我会感激任何指针/嘿/。

bool load(const char *dictionary)
{

 // create root for trie
 //set all children to NULL
 node *root;
 root = malloc(sizeof(node));
 for (int leaves=0; leaves<53; leaves++)
    {
        root ->children[leaves] = NULL;
    }

 //defining an int for the children[x] structure and a char for ever letter to read in
 int children_position=0;
 char * letter = malloc(1);

 //open file 
  FILE *inptr = fopen(dictionary, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", dictionary);
        return false;
    }

   //find size of file

    fseek(inptr, 0L, SEEK_END);
    int size_of_file = ftell(inptr);
    fseek(inptr, 0L, SEEK_SET);

    //loop through dictionary starts here

for (int pointer_position=0; pointer_position<size_of_file; pointer_position++)
{

    //read dictionary one char at the time
    fread(letter,1,1,inptr);

    //if letter is '\'' 

     if (*letter=='\'')
    {
         children_position =52;

//move to next level if path already exists
if (root->children[children_position]!=NULL)
          {
              fprintf(stderr, "children[children_position] exists already \n");
              root = root ->children[children_position];
              break;
          }

          //else create new node 
          else
          {
            fprintf(stderr, "new node created\n");
            node * newnode;
            newnode = malloc(sizeof(node));
            root ->children[children_position] = newnode;
            for (int leaves=0; leaves<53; leaves++)
            {
                newnode ->children[leaves] = NULL;
            }
            newnode->is_word=false;
            root = root ->children[children_position];
            break;
          }
    }

    //if letter is /n set is_word to true
    if (*letter=='\n')
    {
         root->is_word =true;
         // bring root back to original pointer;
    }

    //if letter is an lower case character
   if (islower(*letter))
    {
         children_position = *letter -97;

         //move to next level if path already exists
          if (root->children[children_position]!=NULL)
          {
              fprintf(stderr, "children[children_position] exists already \n");
              root = root ->children[children_position];
              break;
          }

          //else create new node
          else
          {
            fprintf(stderr, "new node created\n");
            node * newnode;
            newnode = malloc(sizeof(node));
            root ->children[children_position] = newnode;
            for (int leaves=0; leaves<53; leaves++)
            {
                newnode ->children[leaves] = NULL;
            }
            newnode->is_word=false;
            root = root ->children[children_position];
            break;
          }
    }


    //if letter is a upper case character
     if (isupper(*letter))
    {
         children_position = *letter- 65+26;

         //move to next level if path already exists
         if (root->children[children_position]!=NULL)
          {
              fprintf(stderr, "children[children_position] exists already \n");
              root = root ->children[children_position];
              break;
          }

           //else create new node
          else
          {
            fprintf(stderr, "new node created\n");
            node * newnode;
            newnode = malloc(sizeof(node));
            root ->children[children_position] = newnode;
            for (int leaves=0; leaves<53; leaves++)
            {
                newnode ->children[leaves] = NULL;
            }
            newnode->is_word=false;
            root = root ->children[children_position];
            break;
          }
    }

}

 fclose(inptr); 
return true;
}

我的结构在头文件中定义如下:

typedef struct node
{
    bool is_word;
    struct node *children[53];
}
node;

0 个答案:

没有答案