Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 无法搜索和删除二进制搜索树_C_Struct_Binary Search Tree_Singly Linked List - Fatal编程技术网

C 无法搜索和删除二进制搜索树

C 无法搜索和删除二进制搜索树,c,struct,binary-search-tree,singly-linked-list,C,Struct,Binary Search Tree,Singly Linked List,我的队友已经搜索了我们这个程序的问题两天了,这个程序应该删除用户输入的作者姓名的信息库。同时,用户需要根据用户输入的作者姓名搜索信息,但程序突然转向b错误并停止。一个精通C语言的人,我需要你的指导 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> struct node{ char title[50], author[50],

我的队友已经搜索了我们这个程序的问题两天了,这个程序应该删除用户输入的作者姓名的信息库。同时,用户需要根据用户输入的作者姓名搜索信息,但程序突然转向b错误并停止。一个精通C语言的人,我需要你的指导

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
    char title[50], author[50], ISBN[50], pubDate[20];
    struct node* left, * right;
};

struct node* insert_node(struct node* root, char author[], char title[], char ISBN[], char pubDate[]);
struct node* FindMin(struct node*);

void in_display(struct node* root);
void search(struct node* root, char searchAuthor[]);
struct node* delete_node(struct node*, char deleteAuthor[]);

void main()
{
    struct node* root = NULL;
    int ch;
    char title[50], author[50], ISBN[50], pubDate[20], deleteAuthor[50], searchAuthor[50];
    do
    {
        printf("\n1.Insert a new Boook Record");
        printf("\n2.Display all the Books record");
        printf("\n3.Delete a Book Record");
        printf("\n4.Search a abook Record ");
        printf("\n5.Exit");
        printf("\nEnter your choice");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:printf("\nEnter the author of the book\t");
            scanf("\n%[^\n]%*c", &author);
            printf("\nEnter the title of the book\t");
            scanf("\n%[^\n]%*c", &title);
            printf("\nKey in the ISBN code\t");
            scanf("\n%[^\n]%*c", &ISBN);
            printf("\nEnter the Publication Date of the book\t");
            scanf("\n%[^\n]%*c", &pubDate);
            root = insert_node(root, author, title, ISBN, pubDate);
            break;
        case 2:
            in_display(root);
            break;
        case 3:
            printf("\nEnter the author of the book to be deleted\t");
            scanf("\n%[^\n]%*c", &deleteAuthor);
            root = delete_node(root, deleteAuthor);
            break;
        case 4:
            printf("\nEnter the author of the book to be searched\t");
            scanf("\n%[^\n]%*c", &searchAuthor);
            search(root, searchAuthor);
            break;
        case 5:
            exit(0);
            break;
        }
    } while (1);
}
struct node* insert_node(struct node* root, char author[], char title[], char ISBN[], char pubDate[])
{
    if (root == NULL)
    {
        struct node* temp = (struct node*)malloc(sizeof(struct node));
        strcpy(temp->author, author);
        strcpy(temp->title, title);
        strcpy(temp->ISBN, ISBN);
        strcpy(temp->pubDate, pubDate);
        temp->left = NULL;
        temp->right = NULL;
        return temp;
    }
    if (author <= root->author)
    {
        root->left = insert_node(root->left, author, title, ISBN, pubDate);
    }
    else
    {
        root->right = insert_node(root->right, author, title, ISBN, pubDate);
    }
    return root;
}
void in_display(struct node* root) {
    if (root == NULL) {
        return;
    }
    in_display(root->left);
    printf("\nAuthor Name - %s", root->author);
    printf("\nBook Title - %s", root->title);
    printf("\nISBN - %s", root->ISBN);
    printf("\nPublication Date - %s\n", root->pubDate);
    in_display(root->right);
}
struct node* delete_node(struct node* root, char deleteAuthor[]) {
    if (root == NULL) {
        return root;
    }
    else if (deleteAuthor < root->author) {
        root->left = delete_node(root->left, deleteAuthor);
    }
    else if (deleteAuthor > root->author) {
        root->right = delete_node(root->right, deleteAuthor);
    }
    else {
        if (root->left == NULL && root->right == NULL) {
            free(root);
            root = NULL;
        }
        else if (root->left == NULL) {
            struct node* temp = root;
            root = root->right;
            free(temp);
            temp = NULL;
        }
        else if (root->right == NULL) {
            struct node* temp = root;
            root = root->left;
            free(temp);
            temp = NULL;
        }
        else {
            struct node* temp = root;
            root->left = FindMin(root);
            root->left->right = root->right;
            root = root->left;
            strcpy(temp->author, root->author);
            strcpy(temp->title, root->title);
            strcpy(temp->ISBN, root->ISBN);
            strcpy(temp->pubDate, root->pubDate);
            free(temp);
            temp = NULL;
        }
        return root;
    }
    return root;
}

struct node* FindMin(struct node* root) {
    while (root->left != NULL) {
        root = root->left;
    }
    return root;
}

void search(struct node* root, char searchAuthor[]) {
    if (root->author > searchAuthor) {
        search(root->left, searchAuthor);
    }
    else if (root->author < searchAuthor) {
        search(root->right, searchAuthor);
    }
    else {
        printf("\nAuthor Name - %s", root->author);
        printf("\nBook Title - %s", root->title);
        printf("\nISBN - %s", root->ISBN);
        printf("\nPublication Date - %s\n", root->pubDate);
    }
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
结构节点{
字符标题[50],作者[50],ISBN[50],出版日期[20];
结构节点*左、*右;
};
结构节点*插入节点(结构节点*根,字符作者[],字符标题[],字符ISBN[],字符发布日期[]);
结构节点*FindMin(结构节点*);
在显示中无效(结构节点*根节点);
无效搜索(结构节点*根,字符搜索作者[]);
结构节点*删除节点(结构节点*,字符删除作者[]);
void main()
{
结构节点*root=NULL;
int-ch;
字符标题[50],作者[50],ISBN[50],发布日期[20],删除作者[50],搜索作者[50];
做
{
printf(“\n1.插入新的Boook记录”);
printf(“\n2.显示所有图书记录”);
printf(“\n3.删除图书记录”);
printf(“\n4.Search a abook Record”);
printf(“\n5.退出”);
printf(“\n输入您的选择”);
scanf(“%d”和“ch”);
开关(ch)
{
案例1:printf(“输入本书作者”);
scanf(“\n%[^\n]]*c”,作者(&A);
printf(“\n输入书的标题”);
scanf(“\n%[^\n]%*c”和标题);
printf(“\nKey在ISBN代码中\t”);
scanf(“\n%[^\n]]*c”和&ISBN);
printf(“\n输入本书的出版日期”);
scanf(“\n%[^\n]%*c”和publidate);
root=插入节点(root、作者、标题、ISBN、pubDate);
打破
案例2:
in_显示(根);
打破
案例3:
printf(“\n输入要删除的书的作者\t”);
scanf(“\n%[^\n]]*c”,删除作者(&D);
root=delete_节点(root,deleteAuthor);
打破
案例4:
printf(“\n输入要搜索的书的作者\t”);
scanf(“\n%[^\n]]*c”,搜索作者(&searchAuthor);
搜索(root,searchAuthor);
打破
案例5:
出口(0);
打破
}
}而(1),;
}
结构节点*插入节点(结构节点*根,字符作者[],字符标题[],字符ISBN[],字符发布日期[])
{
if(root==NULL)
{
结构节点*temp=(结构节点*)malloc(sizeof(结构节点));
strcpy(临时->作者,作者);
strcpy(临时->标题,标题);
strcpy(temp->ISBN,ISBN);
strcpy(temp->pubDate,pubDate);
temp->left=NULL;
temp->right=NULL;
返回温度;
}
如果(作者)
{
root->left=插入_节点(root->left,作者,标题,ISBN,pubDate);
}
其他的
{
root->right=插入_节点(root->right,作者,标题,ISBN,pubDate);
}
返回根;
}
显示中为空(结构节点*根){
if(root==NULL){
返回;
}
in_显示(根->左);
printf(“\n作者名称-%s”,根->作者);
printf(“\n书籍标题-%s”,根目录->标题);
printf(“\nISBN-%s”,根->ISBN);
printf(“\n发布日期-%s\n”,根->发布日期);
in_显示(根->右);
}
结构节点*删除节点(结构节点*根,char deleteAuthor[]){
if(root==NULL){
返回根;
}
else if(deleteAuthorauthor){
根->左=删除_节点(根->左,删除作者);
}
else if(删除作者>根目录->作者){
root->right=delete_节点(root->right,deleteAuthor);
}
否则{
if(root->left==NULL&&root->right==NULL){
自由根;
root=NULL;
}
else if(root->left==NULL){
结构节点*temp=root;
根=根->右;
免费(临时);
温度=零;
}
else if(root->right==NULL){
结构节点*temp=root;
根=根->左;
免费(临时);
温度=零;
}
否则{
结构节点*temp=root;
root->left=FindMin(root);
根->左->右=根->右;
根=根->左;
strcpy(临时->作者,根->作者);
strcpy(临时->标题,根->标题);
strcpy(temp->ISBN,root->ISBN);
strcpy(temp->pubDate,root->pubDate);
免费(临时);
温度=零;
}
返回根;
}
返回根;
}
结构节点*FindMin(结构节点*根){
while(根->左!=NULL){
根=根->左;
}
返回根;
}
无效搜索(结构节点*根,字符搜索作者[]){
如果(根->作者>搜索作者){
搜索(根->左,搜索作者);
}
else if(root->author右,搜索作者);
}
否则{
printf(“\n作者名称-%s”,根->作者);
printf(“\n书籍标题-%s”,根目录->标题);
printf(“\nISBN-%s”,根->ISBN);
printf(“\n发布日期-%s\n”,根->发布日期);
}
}
这也适用于守则中的其他地方,例如

else if (deleteAuthor < root->author)
else if(deleteAuthorauthor)
这也是错误的


顺便说一句:使用
scanf
扫描字符数组时,不需要将
&
放在数组名称前面。只需直接使用数组名。

发布一个显示错误的简短输入序列。解释发生了什么以及您预期会发生什么我将“if(deleteAuthorauthor)”更改为“if(strcmp(searchAuthor,root->author)<0)”,但当搜索未找到时,我不断收到错误消息。我不知道我的if-else语句是否有问题
if (strcmp(author, root->author) <= 0)
else if (deleteAuthor < root->author)