微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

trie数据结构插入功能不起作用为什么?

如何解决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;
    }
  }
}

解决方法

对于初学者来说,函数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<26而不是ch < 27,而数据成员branch有27个元素

struct node *branch[27];

函数insert_trie

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

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

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);

调用未定义的行为。

您需要设置程序文本的格式。格式错误通常是错误的原因。

,
 char *newenty;
  ….
 scanf("%s",newenty);
 root=insert_trie(root,newenty);

newenty没有指向有效的内存,请按如下所示为其分配内存。

  char *newenty = malloc(maxLength);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。