title: Can't get my insert function to work in alphabetic binary string tree
body: <p>I am trying to build a binary tree that will hold words from a file alphabetically as well as count the number of occurrences of the word in the file. Then later on I have to be able to replace words in the original text file. For now I'm just trying to setup my binary tree and getting the words in there. The string tokenizing works and it will print the words and punctuation every line. I will have to store the punctuation in a character array as well and count the occurrences of that. I'm having problems with my insert function but I'm not sure what I'm doing wrong. I am getting a segmentation fault. </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  /*
  Name: Marcus Lorenzana
  */
  
  //binary tree struct to hold left and right node
  //as well as the word and number of occurrences
  typedef struct node
  {
      char* word;
      int count;
      struct node *left;
      struct node *right;
  }
  node;
  
  node *insert(node *item, char *word);
  char* readFile(char* filename);
  
  int main()
  {
      FILE *fin;
      char *word;
      fin = fopen("data.txt", "r");
  
  
      char* filecontents = readFile("data.txt");
  
      //create dictionary node
      node *dictionary; 
      dictionary = NULL;
  
      //read words and punctuation in from the text file
      word = strtok (filecontents, " \n");
      int i = 0;
      while (word != NULL)
      {
  
          printf("%s\n",word);
          insert(dictionary,word);
          printf("%s",dictionary-&gt;word); 
          word = strtok (NULL, " \n");
          i++;
      }
  
  
  
  
      return 0;
  }
  
  //not sure if works
  node *insert(node *item, char *word)
  {
      if(item==NULL)
      {
          item= (node*) malloc(sizeof(node));
          strcpy(item-&gt;word, word);
          item-&gt;left=NULL;
          item-&gt;right=NULL;
          item-&gt;count++;
      }
      else
      {
          if(strcmp(word, item-&gt;word)&lt;0)
          {
              item-&gt;left=insert(item-&gt;left, word); 
              item-&gt;count++;
          }
          else if(strcmp(word, item-&gt;word)&gt;0)
          {
              item-&gt;right=insert(item-&gt;right, word);
              item-&gt;count++;
          }
          else
          {
              item-&gt;count++;
          }
      }
      return item;
  }
  
  
  char* readFile(char* filename)
  {
      FILE* file = fopen(filename,"r");
      if(file == NULL)
      {
          return NULL;
      }
  
      fseek(file, 0, SEEK_END);
      long int size = ftell(file);
      rewind(file);
  
      char* content = calloc(size + 1, 1);
  
      fread(content,1,size,file);
  
      return content;
  }
  </code></pre>