c-无法搜索和删除二进制搜索树



我的队友已经搜索了我们这个程序的问题两天了,这个程序假设删除了用户输入的基于作者名称的信息。同时,用户需要根据用户输入的作者姓名搜索信息,但程序突然出现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 bookt");
scanf("n%[^n]%*c", &author);
printf("nEnter the title of the bookt");
scanf("n%[^n]%*c", &title);
printf("nKey in the ISBN codet");
scanf("n%[^n]%*c", &ISBN);
printf("nEnter the Publication Date of the bookt");
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 deletedt");
scanf("n%[^n]%*c", &deleteAuthor);
root = delete_node(root, deleteAuthor);
break;
case 4:
printf("nEnter the author of the book to be searchedt");
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 - %sn", 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 - %sn", root->pubDate);
}
}
if (author <= root->author)

不是比较两个字符串,而是比较两个指针。

请改用strcmp

if (strcmp(author, root->author) <= 0)

这也适用于代码中的其他位置,例如

else if (deleteAuthor < root->author)

也是错误的。

BTW:当使用scanf扫描到char数组时,不需要将&放在数组名称前面。只需直接使用数组名称。

最新更新