title: segmentation fault (core dumped) in binary search tree
body: <p>I am trying to implement a r&amp;b tree, but firstly I want a simple binary tree (which does not save content on its leaves) and then implement r&amp;b properties. Problem is I get segmentation fault I cannot explain.</p>
  
  <p>Program is as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdbool.h&gt;
  
  typedef struct node
  {
      unsigned long int val;
      bool black;
      struct node* parent;
      struct node* lchild;
      struct node* rchild;
  }mynode;
  
  mynode* createNode(unsigned long int ival, mynode* father);
  mynode* createLeaf(unsigned long int ival, mynode* father);
  mynode* search (unsigned long int ival, mynode *root);
  void insert ( unsigned long int ival, mynode *root);
  
  
  int main()
  {
      mynode root;
      mynode *rootptr;
      mynode *leafptr;
      FILE *fp;
      int ch;
      unsigned long long lines=0, i=0;
      unsigned long *myArr;
      unsigned long int ival;
  
     fp = fopen("integers.txt","r");
     if(fp == NULL)
     {
        printf("Error in opening file.");
        return(-1);
     }
  
      while(!feof(fp))
      {
      ch = fgetc(fp);
      if(ch == '\n')
      {
          lines++;
      }
      }
      lines++;
      printf("lines = %lu", lines);
      myArr = (unsigned long*)calloc(lines, sizeof(unsigned long));
  
      fseek(fp, 0, SEEK_SET);
      while(!feof(fp))
      {
            fscanf(fp, "%lu,", &amp;myArr[i] ); // des ta pos k giati tou input.
            i++;
      }
      fclose(fp);
  
      root.val = myArr[0];
      root.parent = NULL;
      root.lchild = NULL;
      root.rchild = NULL;
      root.black = true;
      rootptr = &amp;root;
      leafptr = createLeaf(rootptr-&gt;val, rootptr);
      rootptr-&gt;lchild = leafptr;
      leafptr = createLeaf(rootptr-&gt;val, rootptr);
      rootptr-&gt;rchild = leafptr;
      for(i=1; i&lt;lines; i++)
      {
          ival = myArr[i];
          insert(ival, rootptr);
      }
  
      return 0;
  }
  
  mynode* createNode(unsigned long int ival, mynode* father)
  {
    mynode* nodeptr;
    mynode node;
    nodeptr = &amp;node;
    nodeptr-&gt;val = ival;
    nodeptr-&gt;lchild = NULL;
    nodeptr-&gt;rchild = NULL;
    nodeptr-&gt;parent = father;
    nodeptr-&gt;black = true;
    return nodeptr;
  }
  
  mynode* createLeaf(unsigned long int ival, mynode* father)
  {
    mynode* nodeptr;
    mynode leaf;
    nodeptr = &amp;leaf;
    nodeptr-&gt;val = ival;
    nodeptr-&gt;lchild = NULL;
    nodeptr-&gt;rchild = NULL;
    nodeptr-&gt;parent = father;
    nodeptr-&gt;black = true;
    return nodeptr;
  }
  
  mynode* search (unsigned long int ival, mynode *rootptr)
  {
      mynode* myptr;
  
      myptr = rootptr;
  
      while ( ( (myptr-&gt;lchild) != NULL) &amp;&amp; ( (myptr-&gt;rchild) != NULL))
      {
          if ( ival &lt; myptr-&gt;val)
          {
              myptr = myptr-&gt;lchild;
          }
          else
          {
              myptr = myptr-&gt;rchild;
          }
      }
      return myptr;
      }
  
  void insert (unsigned long int ival, mynode *root)
  {
      mynode * current;
      mynode * leafptr;
      mynode * father;
      unsigned long int max, min;
      unsigned long int num;
  
      current = search(ival, root);
      num = current-&gt;val;
      if((current-&gt;val) == ival)
      {
          return ;
      }
      else
      {
          if(ival&gt;(current-&gt;val))
          {
              max = ival;
              min = current-&gt;val;
          }
          else
          {
              max = current-&gt;val;
              min = ival;
          }
          father = current-&gt;parent;
          current = createNode(min, father);
          if(num == (father-&gt;lchild)-&gt;val)
          {
              father-&gt;lchild = current;
          }
          else
          {
              father-&gt;rchild = current;
          }
          leafptr = createLeaf(min, current);
          current-&gt;lchild = leafptr;
          leafptr = createLeaf(max, current);
          current-&gt;rchild = leafptr;
         return ;
      }
  }
  </code></pre>
  
  <p>I open a file. Count the number of lines, because I know each line has one number. Create an array using the above information. I then create the root and its 2 leaves. And then I insert (there I get segmentantion fault) rest of the array to my data structure. I think problem lies in the functions.</p>
  
  <p><a href="https://drive.google.com/open?id=1HNxM7J100BvTr1U8X8es7wtZ9_2wy35y" rel="nofollow noreferrer">Here is the text file with numbers. </a></p>