user1179321
user1179321

Reputation: 801

Binary Tree in C

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.

#include <stdio.h>
#include <stdlib.h>


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->aBook)->year = year;
        (nodeP->aBook)->name = name;
        /* initialize the children to null */
        (nodeP)->left = NULL;    
        (nodeP)->right = NULL;  
    }
    else if(year > (nodeP->aBook)->year)
    {
        addBook(&(nodeP)->left,name,year );
    }
    else if(year < (nodeP->aBook)->year)
    {
        addBook(&(nodeP)->right,name,year );
    }
    return nodeP;
}

void freeBTree(BTree* books)
{
    if( books != NULL )
    {
        freeBTree(books->left);
        freeBTree(books->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);*/
}

Upvotes: 0

Views: 3008

Answers (2)

Scooter
Scooter

Reputation: 7061

One problem is that you aren't initializing to NULL:

BTree *head;

should be

BTree *head = NULL;

I would recommend setting your compiler warnings higher. Your two recursive calls are not right and the compiler should have warned about them:

addBook(&(nodeP)->left,name,year );

should be:

addBook( nodeP->left,name,year );

from a parameter passing standpoint. However, this function won't work as it is right now since you are adding when the node pointer is NULL, which means you can't attach a parent to a child since the parent pointer node is gone. I think the logic should look at the applicable right/left node and if NULL, add right there in the routine, else call recursively till a node is found that has a NULL right/left pointer.

Something like this:

BTree *makeNode(char *name, int year)
{
   // NOTE: 3 frees required for every node
   BTree *nodeP = malloc( sizeof( struct tnode ) );  // 1
   nodeP->aBook = malloc( sizeof(struct book) );     // 2
   (nodeP->aBook)->year = year;
   (nodeP->aBook)->name = malloc(strlen(name) + 1);  // 3
   strcpy((nodeP->aBook)->name,name);
   /* initialize the children to null */
   nodeP->left = NULL;    
   nodeP->right = NULL;  
   return nodeP;
}

BTree* addBook(BTree* nodeP, char* name, int year)
{
    if ( nodeP == NULL )
    {
        nodeP = makeNode(name,year);
    }
    else if (year > (nodeP->aBook)->year)
    {
       if ( nodeP->left == NULL )
          nodeP->left = makeNode(name,year);
       else
          addBook( nodeP->left,name,year );
    }
    else if(year < (nodeP->aBook)->year)
    {
       if ( nodeP->right == NULL )
          nodeP->right = makeNode(name,year);
       else
          addBook( nodeP->right,name,year );
    }
    return nodeP;
}

void printBooks(BTree* books) 
{
    if (books != NULL) {
       printf("book: %s %d\n",books->aBook->name,books->aBook->year);
       printBooks(books->right);
       printBooks(books->left);
    }
}

Upvotes: 1

Karoly Horvath
Karoly Horvath

Reputation: 96258

You're trying to access an uninitalized pointer nodeP->aBook:

nodeP = (struct tnode*) malloc( sizeof( struct tnode ) );
(nodeP->aBook)->year = year;
  • You have to allocate space with malloc.
  • or, store the data directly in the node (with a struct, and not a pointer to a struct).

Upvotes: 2

Related Questions