0

我的问题是这样的:我想搜索一棵字符串基数树,每个字符串都有一个唯一的数字,直到找到具有作为函数参数给出的数字的那个。当我递归搜索树时,我需要更新字符串(当我下去时将当前字符串与新字符串连接起来,并在我从重复中返回时切断后缀)。每个树节点都有一个指向其子节点和兄弟节点的指针。该功能如下,由于某种原因它不起作用。

void prev(struct tree *t, int w, int start, int end) {
  char *newWord = "";
  bool f = false;
  bool *found = &f;

  void prevRec(struct tNode *t, 
    int w, int start, int end, char *soFar) {
    if (t != NULL) {
      char *updatedWord = malloc(strlen(soFar) + strlen(t->word) + 1);
      strcpy(updatedWord,soFar);
      strcat(updatedWord,t->word);
      int length = strlen(t->word);
      if (t->count == w) {
        *found = true;
        if ((start > -1) && (start <= end)) {
          newWord = updatedWord;
        } else newWord = "";
      } else {
        if (!*found) {
          prevRec(t->child,w,start,end,updatedWord);
          char *sub = substring(updatedWord, 0, strlen(updatedWord) - length);
          free(updatedWord);
          updatedWord = sub;
          prevRec(t->brother,w,start,end,updatedWord);
          sub = substring(updatedWord, 0, strlen(updatedWord) - length);
          free(updatedWord);
          updatedWord = sub;
        }
      } 
    }
  } //prevRec

  prevRec(t->root->child,w,start,end,newWord);
  if (strlen(newWord) < 1 || end > strlen(newWord)) {
    printf("ignored\n");
    globalFound = 1;
  } else {
    char *tmp = substring(newWord,start,end - start + 1);
   insert(t,tmp); 
   free(tmp);
  }
} 
4

0 回答 0