c - 尝试使用扫描值在 C 中创建二叉搜索树

标签 c binary-search-tree

我在这方面遇到了很多麻烦。我看过很多例子和类似的实现,但我仍然不明白为什么这不起作用。由于某种原因,我的代码正在获取每个新值并将其作为新头,并从本质上删除以前的树。它不是插入值,而是每次都说 head 为 null 并重写存储在 head 中的值...以下是我的代码:

typedef struct studentRec{
    int id;
    char name[25]; //the name has a maximum length of 25 letters
    char major[15]; //the major array has a max length of 15
    int year;
    struct studentRec *left, *right;
}student;

student* createNode(int ID, char *name, char *major, int year);
student* initBST(FILE *input, char *argv);
student* addNode(student *head, int ID, char *name, char *major, int year);
int Search(student *head);
void printInorder(student *n);


int main(int argc, char *argv[]){
FILE *input;
printf("This is the name of input: %s\n", argv[1]);
student *mainHead = malloc(sizeof(student*));
mainHead = initBST(input, argv[1]);
printInorder(mainHead);
return 0;
}

student* initBST(FILE *input, char *argv){
    int ID = -1;
    input = fopen(argv, "r");
    fscanf(input, "%d", &ID);
    student *head = malloc(sizeof(student*));
    int grade = -1;
    while(ID != 0){ 
        char *first, *last, *major, *name;
        first = malloc(25*sizeof(char));
        last = malloc(25*sizeof(char));
        major = malloc(25*sizeof(char));
        name = malloc(25*sizeof(char));
        fscanf(input, "%d", &ID);
        if(ID == 0){
        break;
        }
        fscanf(input, "%s %s  %s  %d", first, last, major, &grade);
        sprintf(name, "%s %s", first, last);

        head = addNode(head, ID, name, major, grade);
    }
    return head;
}

void printInorder(student *n){
     if(n != NULL){
         printInorder(n->left);
         printf("This is the current value of ID: %d\n", n->id);
         printInorder(n->right);
     }
}

student* createNode(int ID, char *name, char *major, int year){
            printf("Adding this ID value: %d\n", ID);
            student *new = malloc(sizeof(student*));
            new->left = NULL;
            new->right = NULL;
            new->id = ID;
            strcpy(new->name, name);
            strcpy(new->major, major);
            new->year = year;
            return new;
}

student* addNode(student *head, int ID, char *name, char *major, int year){ 
    if(head == NULL){
        printf("head == NULL\n");
        return createNode(ID, name, major, year);
    }
    else{
        if(ID < head->id){
            return head->left = addNode(head->left, ID, name, major, year); 
        }
        else if(ID > head->id){
            return head->right = addNode(head->right, ID, name, major, year); 
        } 
    }
}

最佳答案

第一次调用 addNode 似乎工作正常并创建了头。但是,当 initBST 第二次调用 addNode 时,addNode 会在以下两行之一上重复出现:

    if(ID < head->id){
        return head->left = addNode(head->left, ID, name, major, year); 
    }
    else if(ID > head->id){
        return head->right = addNode(head->right, ID, name, major, year); 
    } 

由于 head->left 和 head->right 均为 NULL,因此 addNode 的新迭代将打印 head == NULL 消息,但它会正确创建新节点。 addNode 的另一个迭代(运行上面的代码)正确设置 head->left 或 head->right,但它随后返回该值,initBST 将其设置为新头。 addNode 的相关部分应该如下所示:

head->left = addNode(head->left, ID, name, major, year);
return head;
// with the other one changed as well

关于c - 尝试使用扫描值在 C 中创建二叉搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27094573/

相关文章:

c++ - 获取二叉树的高度时出现堆栈溢出异常

c - 实现广度优先搜索时的错误 (C)

c - 名称中包含目标文件的 GNU Makefile 默认值变量

c - MPI 中的问题 -> 地址 : (nil) 失败

c - 我的 Mandelbrot 集有奇怪的文物

c - 如何根据列表表示图的边?

algorithm - 一个数字作为叶节点出现了多少次?

无法使用 C 中的套接字下载多个文件

algorithm - 基于左右子树大小的平衡二叉搜索树

c - 为什么这个程序总是从二叉搜索树中打印相同的节点