Trie实现逻辑错误?

时间:2017-11-03 04:52:10

标签: c trie cs50

我目前正在开发一个trie实现:

  1. 从文本文件中读取单词
  2. 逐个字符地逐字逐句地翻译
  3. 将该字符的字母索引号附加到新节点并将其附加到根节点
  4. 我在第3步遇到了麻烦。

    你知道,我在第三步中要做的是:

    1. 如果需要,则创建一个新节点(如果需要节点已存在,请跳过步骤2)
    2. 将新节点附加到root的children数组,其中它连接到扫描字符的字母索引的位置,例如。如果" a"要被扫描,新节点将附加到root->子项[按字母顺序索引
    3. 移至root中的下一个节点,以便下一个节点将附加到该节点
    4. 为了进一步缩小问题范围,我仍然想弄清楚第二个第3和第4个问题。

      对于第二步,我到目前为止尝试的是:

      if(root->children[index] == NULL) root->children[index] = makeNewNode();
      

      假设rootindex已经定义,makeNewNode()将新节点初始化为NULL

      第三步,我做了:

      root = root->children[index];
      

      设置root以使其现在成为下一个节点

      我是否在这些陈述中犯了任何逻辑错误?

      trie节点结构和根声明

      typedef struct trieNode
      {
          bool endOfWord;
          // 27 not 26 because an apostrophe also counts as a character
          struct trieNode *children[27];
      
      } trieNode;
      
      //Make a new node function prototype.
      trieNode* makeNewNode();
      
      trieNode *root;
      

      加载功能

      bool load(const char *dictionary)
      {
          // open dictionary file
          FILE *file = fopen(dictionary, "r");
      
          // check if file opened successfully
          if(file == NULL)
          {
              fprintf(stderr, "Can't open file.\n");
              unload();
              return false;
          }
      
          // initialize root
          root = makeNewNode();
      
          char word[LENGTH + 1];
          int index = 0;
      
          // load a word in line by line
          while(fscanf(file, "%s", word) != EOF)
          {
              printf("Word loaded %s\n", word);
      
              // scan loaded word character by character
              for(int i = 0; i < strlen(word); i++)
              {
                  // remove any capital letters
                  if(isalpha(word[i]) && isupper(word[i])) tolower(word[i]);
      
                  // set current character in word to it's alphabetical index (apostrphes index is 26)
                  index = word[i] - 'a';
                  if(word[i] == '\'') index = 26;
      
                  printf("Char being searched %c Index %i\n", word[i], index);
      
                  // if character does not exist, create a new node and point root to it
                  if(root->children[index] == NULL) root->children[index] = makeNewNode();
      
                  // move to next node
                  root = root->children[index];
      
                  printf("Children's value: %p\n", (void *) root->children[index]);
              }
          // indicate leaf or end of branch
          root->endOfWord = true;
      }
      
      // close dictionary
      fclose(file);
      
      return true;
      }
      

      makeNewNode函数

      // function to automate initialization of nodes
      trieNode* makeNewNode()
      {
          // give some space for the new node
          struct trieNode* newNode = malloc(sizeof(trieNode));
      
          newNode->endOfWord = false;
      
          // initialize all children pointers to NULL.
          for (int i = 0; i < 27; i++) newNode->children[i] = NULL;
      
          return newNode;
      }
      

1 个答案:

答案 0 :(得分:0)

 if(root->children[index] == NULL) root->children[index] = makeNewNode();

            // move to next node
            root = root->children[index];

您可以在此处修改root。对于下一个wordroot不是实际的根,而是插入数据的最后一个节点。您需要维护原始根。