0

我在用 C 语言构建二叉树时遇到了麻烦。我想能够将书籍添加到树中,将较晚出版年份的书籍添加到左侧,将较早出版年份的书籍添加到右侧。我不断收到运行错误,我不确定为什么。

#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);*/
}
4

2 回答 2

2

您正在尝试访问未初始化的指针nodeP->aBook

nodeP = (struct tnode*) malloc( sizeof( struct tnode ) );
(nodeP->aBook)->year = year;
  • 您必须使用malloc.
  • 或者,将数据直接存储在节点中(使用结构,而不是指向结构的指针)。
于 2012-09-24T23:56:48.763 回答
1

一个问题是您没有初始化为 NULL:

BTree *head;

应该

BTree *head = NULL;

我建议将编译器警告设置得更高。您的两个递归调用不正确,编译器应该警告它们:

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

应该:

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

从参数传递的角度来看。但是,这个函数不会像现在这样工作,因为您在节点指针为 NULL 时添加,这意味着您不能将父节点附加到子节点,因为父指针节点已经消失。我认为逻辑应该查看适用的右/左节点,如果为 NULL,则在例程中添加右,否则递归调用,直到找到具有 NULL 右/左指针的节点。

像这样的东西:

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);
    }
}
于 2012-09-24T23:57:12.097 回答