Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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);*/
- }
- BTree *head;
- BTree *head = NULL;
- addBook(&(nodeP)->left,name,year );
- addBook( nodeP->left,name,year );
- 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 %dn",books->aBook->name,books->aBook->year);
- printBooks(books->right);
- printBooks(books->left);
- }
- }
- nodeP = (struct tnode*) malloc( sizeof( struct tnode ) );
- (nodeP->aBook)->year = year;
Add Comment
Please, Sign In to add comment