title: Binary Tree in C
body: <p>I'm having trouble building a binary tree in C. I'm suppose to be able to add books to the tree where books with a later publishing year get added to the left and earlier publishing year gets added to the right. I keep getting a run error and i'm not really sure why.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  struct book { 
      char* name; 
      int year;
  };
  
  typedef struct tnode { 
      struct book *aBook; 
      struct tnode *left; 
      struct tnode *right;
  } BTree;
  
  BTree* addBook(BTree* nodeP, char* name, int year){
      if( nodeP == NULL )
      {
          nodeP = (struct tnode*) malloc( sizeof( struct tnode ) );
          (nodeP-&gt;aBook)-&gt;year = year;
          (nodeP-&gt;aBook)-&gt;name = name;
          /* initialize the children to null */
          (nodeP)-&gt;left = NULL;    
          (nodeP)-&gt;right = NULL;  
      }
      else if(year &gt; (nodeP-&gt;aBook)-&gt;year)
      {
          addBook(&amp;(nodeP)-&gt;left,name,year );
      }
      else if(year &lt; (nodeP-&gt;aBook)-&gt;year)
      {
          addBook(&amp;(nodeP)-&gt;right,name,year );
      }
      return nodeP;
  }
  
  void freeBTree(BTree* books)
  {
      if( books != NULL )
      {
          freeBTree(books-&gt;left);
          freeBTree(books-&gt;right);
          //free( books );
      }
  }
  
  void printBooks(BTree* books){
      if(books != NULL){
  
      }
  }
  
  int main(int argc, char** argv) {
      BTree *head;
      head = addBook(head,"The C Programming Language", 1990);
      /*addBook(head,"JavaScript, The Good Parts",2008);
      addBook(head,"Accelerated C++: Practical Programming by Example", 2000);
      addBook(head,"Scala for the impatient",2012);*/
  }
  </code></pre>