【问题标题】:trie data structure insert function is not working. Why?trie 数据结构插入功能不起作用。为什么?
【发布时间】:2021-02-22 07:39:15
【问题描述】:

我已经实现了一个 trie 数据结构 (reference)。当我插入数据结构时,出现分段错误。这可能是语义错误。请帮忙改正。

#include <stdio.h>
#include <stdlib.h>
#define maxlength 10

typedef struct node {
  int isend;
  struct node *branch[27];
} trinode;

int count, len;

trinode *createnode() {
  trinode *new = (trinode *)malloc(sizeof(trinode));
  int ch;
  for (ch = 0; ch < 26; ch++) {
    new->branch[ch] = NULL;
  }
  new->isend = 0;
}

trinode *insert_trie(trinode *root, char *newenty) {
  int ind;
  trinode *proot;
  if (root == NULL)
    root = createnode();
  proot = root;
  for (int i = 0; i < maxlength; i++) {
    ind = newenty[i] - 'a';
    if (newenty[i] == '\0')
      break;
    else {
      if (root->branch[ind] == NULL)
        root->branch[ind] = createnode();
      root = root->branch[ind];
    }
    if (root->isend != 0)
      printf("trying to insert duplicate");
    else
      root->isend = 1;
    return proot;
  }
}

void print_trie(trinode *cur) {
  char word[40];

  for (int i = 0; i < 26; i++) {
    if (cur->branch[i] != NULL) {
      word[count++] = (i + 'a');
      if ((cur->branch[i]->isend) == 1) {
        printf("\n");
        for (int j = 0; j < count; j++) {
          printf("%c", word[j]);
        }
      }
      print_trie(cur->branch[i]);
    }
  }
  count--;
}

int search_trie(trinode *root, char *target) {
  int ind;
  for (int i = 0; i < maxlength && root; i++) {
    ind = target[i] - 'a';
    if (target[i] == '\0')
      break;
    else
      root = root->branch[ind];
  }
  if (root && root->isend == 1)
    return root;
  else
    return 0;
}
int main() {
  int ch;
  trinode *root = NULL;
  char *newenty;
  char *target;
  int check;

  while (1) {
    printf("\n enter option 1.insert_trie 2.display 3.search 4.exit");
    scanf("%d", &ch);
    switch (ch)

    {
    case 1:
      printf("enter word");
      scanf("%s", newenty);
      root = insert_trie(root, newenty);
      break;

    case 2:
      count = 0;
      print_trie(root);
      break;

    case 3:
      printf("enter elem you want to search");
      scanf("%s", target);
      check = search_trie(root, target);
      if (check == 0)
        printf("word not found");
      else
        printf("found");
      break;

    case 4:
      exit(0);
      break;
    }
  }
}

【问题讨论】:

    标签: c struct tree definition trie


    【解决方案1】:

    对于初学者来说,函数 createnode 什么都不返回

    trinode *createnode()
    {
      trinode *new=(trinode *)malloc(sizeof(trinode));
      int ch;
      for(ch=0;ch<26;ch++)
      {
        new->branch[ch]=NULL;
      }
      new->isend=0;
    }
    

    还不清楚为什么 for 循环中的条件是 ch&lt;26 而不是 ch &lt; 27 而数据成员 branch 有 27 个元素

    struct node *branch[27];
    

    this for 在函数insert_trie

    for(int i=0;i<maxlength;i++)
    

    没有意义,因为在循环中有返回语句

    return proot;
    

    所以循环不超过一次迭代。

    函数print_trie依赖于全局变量count,这是一个非常糟糕的设计,不清楚该函数的作用。

    函数search_trie 声明如下

    int search_trie(trinode *root,char *target)
    

    也就是说它的返回类型为int。然而该函数返回一个trinode*类型的指针:

    if(root && root->isend==1)
       return root;
    

    主要是指针

    char *newenty;
    char *target;
    

    未初始化并且具有不确定的值。因此这些陈述

    scanf("%s",newenty)
    

    scanf("%s",target);
    

    调用未定义的行为。

    你需要格式化程序的文本。错误的格式通常是导致错误的原因。

    【讨论】:

      【解决方案2】:
       char *newenty;
        ….
       scanf("%s",newenty);
       root=insert_trie(root,newenty);
      

      newenty 没有指向有效的内存,如下分配内存。

        char *newenty = malloc(maxLength);
      

      【讨论】:

        猜你喜欢
        • 2016-03-08
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-09
        • 2015-07-16
        • 1970-01-01
        相关资源
        最近更新 更多