搜索错误时收到搜索二叉树

Posted

技术标签:

【中文标题】搜索错误时收到搜索二叉树【英文标题】:Search Binary Tree received when Search is wrong 【发布时间】:2021-08-22 15:52:26 【问题描述】:

我真的无法应付的家伙。我该怎么解决这个问题。我不断收到我的 root 错误,它总是提到 root 是 nullptr。然后,突然间,我所有的书籍记录都消失了/删除。我不知道如何解决这个问题,我一直在使用 strcmp 进行更改,但没有它,但仍然如此。如果我找到搜索过的内容也是如此,它会删除我所有的书籍记录。也许你们可以为我提供一些需要修复的错误语法,并通过编码解释我的错误。 This is the snapshot of the error

#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], searchAuthor[50];
    struct node* left, * right;
;
struct node* delete_node(struct node* root, char deleteAuthor[]);
struct node* insert_node(struct node* root, char author[], char title[], char ISBN[], char pubDate[]);
struct node* FindMin(struct node*);
//struct node* search(struct node* root, char deleteAuthor[]);

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


void main()

    struct node* root = NULL;
    int ch, loop;
    char title[50], author[50], ISBN[50], pubDate[20], searchAuthor[50], deleteAuthor[50];
    printf("\n\n\t\t\tBinary Search Tree");
    printf("\n\t\t-- To stores a library of book records");
    printf("\n\t\t-- To search a library of book records");
    printf("\n\t\t-- To delete a library of book records");
    printf("\n\t\t-- To display a library of book records");
    printf("\n\t=========================================================\n");

    printf("\n\tState the number of book you need to input: ");
    scanf("%d", &loop);
    for (int c = 0; c < loop; c++) 
        printf("\n\n\t\t\t--------Insert New Book--------");
        printf("\n\tEnter the author of the book: \t\t\t");
        scanf("\n%[^\n]%*c", author);
        printf("\tEnter the title of the book: \t\t\t");
        scanf("\n%[^\n]%*c", title);
        printf("\tKey in the ISBN code: \t\t\t\t");
        scanf("\n%[^\n]%*c", ISBN);
        printf("\tEnter the Publication Date of the book: \t");
        scanf("\n%[^\n]%*c", pubDate);
        root = insert_node(root, author, title, ISBN, pubDate);
    
    printf("\t\t\t--------Insert New Book End--------\n\n");
    printf("\n\tPlease enter choice (1/2/3/4/5) based on the ....");
    do
    
        //printf("\n\t1.\tInsert a new Boook Record");
        printf("\n\t1.\tDisplay all the Books record");
        printf("\n\t2.\tDelete a Book Record");
        printf("\n\t3.\tSearch a abook Record ");
        printf("\n\t4.\tExit");
        printf("\n\n\tEnter your choice: ");
        scanf("%d", &ch);
        switch (ch)
        case 1:
            printf("\n\t\t-----------------Library of Book Records-----------------\n");
            in_display(root);
            break;
        case 2:
            printf("\nEnter the author of the book to be deleted: \t");
            scanf("\n%[^\n]%*c", deleteAuthor);
            root = delete_node(root, deleteAuthor);
            break;
        case 3:
            printf("\nEnter the author of the book to be searched: \t");
            scanf("\n%[^\n]%*c", searchAuthor);
            search(root, searchAuthor);
            break;
        case 4:
            exit(0);
            break;
        default: printf("\n\tWrong Option\n");
        
     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 (strcmp(author, root->author)<=0)
    
        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("\n\tAuthor Name \t\t- %s", root->author);
    printf("\n\tBook Title \t\t- %s", root->title);
    printf("\n\tISBN \t\t\t- %s", root->ISBN);
    printf("\n\tPublication Date \t- %s\n", root->pubDate);
    in_display(root->right);

struct node* delete_node(struct node* root, char deleteAuthor[]) 
    if (root == NULL) 
        return root;
    
    else if (strcmp(deleteAuthor, root->author) < 0) 
        root->left = delete_node(root->left, deleteAuthor);
        printf("\nBook Record Not Found\n");
    
    else if (strcmp(deleteAuthor, root->author) > 0) 
        root->right = delete_node(root->right, deleteAuthor);
        printf("\nBook Record Not Found\n");
    
    else 
        if (root->left == NULL && root->right == NULL) 
            free(root);
            root = NULL;
            printf("\nBook Record Successfully Deleted\n");
        
        else if (root->left == NULL) 
            struct node* temp = root;
            root = root->right;
            free(temp);
            temp = NULL;
            printf("\nBook Record Successfully Deleted\n");
        
        else if (root->right == NULL) 
            struct node* temp = root;
            root = root->left;
            free(temp);
            temp = NULL;
            printf("\nBook Record Successfully Deleted\n");
        
        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;
            printf("\nBook Record Successfully Deleted\n");
        
        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) 
        return search(root->right, searchAuthor);
    
    else if (root->author > searchAuthor) 
        return search(root->left, searchAuthor);
    
    else 
        printf("\n\t\t\t--------Searched Book Found--------\n");
        printf("\n\t\t\tAuthor Name \t\t- %s", root->author);
        printf("\n\t\t\tBook Title \t\t- %s", root->title);
        printf("\n\t\t\tISBN \t\t\t- %s", root->ISBN);
        printf("\n\t\t\tPublication Date \t- %s\n", root->pubDate);
        //return root;
    

【问题讨论】:

您发布了相同的问题,只有细微差别here。您的 IDE 屏幕截图中的代码与您在此处发布的代码不匹配。 我猜您当前遇到的错误是您没有检查指针是否为空。 search 不返回任何东西? delete 需要工作;什么是删除返回?我建议将警告提高到更高的级别。 @MOehm 好的,我已经更改了屏幕截图,但在搜索时仍然有问题 @Neil 我已经重新编辑了问题的快照。搜索功能出了点问题 search 被声明为void 时,你怎么能拥有return search(... 【参考方案1】:

您的代码有很多需要改进的地方,但我只关注search 函数:

它应该处理rootNULL 的情况。如果不这样做,当rootNULL 时,您最终将访问root-&gt;leftroot-right,这会产生异常。

字符数组不会像现在这样比较。您的代码当前正在比较指针值。请改用strcmp

当函数类型为void 时使用return 是矛盾的。我建议将函数的返回类型更改为struct * node,并让函数返回找到的节点(如果有),否则返回NULL

所以:

struct node * search(struct node* root, char searchAuthor[])
    if (root == NULL) 
        printf("\n Book not found");
        return NULL;
    
    int diff = strcmp(root->author, searchAuthor);
    if (diff < 0) 
        return search(root->right, searchAuthor);
    
    else if (diff > 0) 
        return search(root->left, searchAuthor);
    
    else 
        printf("\n\t\t\t--------Searched Book Found--------\n");
        printf("\n\t\t\tAuthor Name \t\t- %s", root->author);
        printf("\n\t\t\tBook Title \t\t- %s", root->title);
        printf("\n\t\t\tISBN \t\t\t- %s", root->ISBN);
        printf("\n\t\t\tPublication Date \t- %s\n", root->pubDate);
        return root;
    

对此函数的最后评论:最好从该函数(和其他函数)中删除所有打印,并且只在主代码中进行打印。这使您的函数独立于 I/O 事务,这是一件好事。

【讨论】:

但我只是注意到当它返回 NULL 时,我的所有记录都从节点中删除了。我的 insert_node 或只是这个搜索功能有问题吗? 不适用于search 函数,除非您将返回的值分配给根——当然您不应该这样做(搜索函数永远不会修改树)。我只是专注于你的问题。我的回答不能解决您代码中的所有其他问题。如果您还有其他问题,请通过新问题提出相关问题。 注:你知道:堆栈溢出问题应该只问一个问题,所以我拿了你开始的第一件事:“它总是提到根是 nullptr"(以及您提供的屏幕截图)。

以上是关于搜索错误时收到搜索二叉树的主要内容,如果未能解决你的问题,请参考以下文章

搜索错误时收到搜索二叉树

二叉树二叉搜索树

带你整理面试过程中关于 二叉树二叉搜索树平衡二叉树B 树和 B+树的相关知识

二叉树二叉搜索树中的众数(leetcode501)

二叉树二叉搜索树中的众数(leetcode501)

数据结构~基础2~树《二叉树二叉搜索树AVL树B树红黑树》的设计~高度平衡二叉树AVL树