Achyut Jagini
Achyut Jagini

Reputation: 73

trie data structure insert function is not working. Why?

I have implemented a trie data structure (reference). When I insert into the data structure, I get a segmentation fault. It could be a semantic error. Please help to correct it.

#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;
    }
  }
}

Upvotes: 1

Views: 117

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

For starters the function createnode returns nothing

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

Also it is unclear why the condition in the for loop is ch<26 instead of ch < 27 while the data member branch has 27 elements

struct node *branch[27];

This for in the function insert_trie

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

does not make a sense because within the loop there is the return statement

return proot;

So the loop has no more than one iteration.

The function print_trie depends on the global variable count that is a very bad design and it is unclear what the function does.

The function search_trie is declared like

int search_trie(trinode *root,char *target)

That is it has the return type int. However the function returns a pointer of the type trinode*:

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

In main the pointers

char *newenty;
char *target;

are not initialized and have indeterminate values. Thus these statements

scanf("%s",newenty)

and

scanf("%s",target);

invoke undefined behavior.

And you need to format the text of the program. A bad formatting is usually a reason of bugs.

Upvotes: 4

kiran Biradar
kiran Biradar

Reputation: 12732

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

newenty isn't pointing to valid memory, allocate memory to it like below.

  char *newenty = malloc(maxLength);

Upvotes: 3

Related Questions