C language:Removing 来自字符串 BST 的一个节点

C language:Removing a node from a BST of strings

我正在尝试编写一个函数,它将从 BST 中删除一个节点,该节点将 == 包含用户在程序中插入的单词,这意味着我询问用户,如果他想删除某个单词,他会输入该单词并将其保存在 main 中的变量 key[MAX] 下。但是当试图删除节点时出了点问题,所以我的程序就死了

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 15

typedef struct BST
{
    char data[MAX];
    struct BST *left;
    struct BST *right;
    int count;
} node;

node *create();
void insert(node *,node *);
void preorder(node *);
struct BST *minValueNode(struct BST *node)
{
    struct BST *current = node;


    while (current && current->left != NULL)
        current = current->left;

    return current;
}
struct BST *deleteNode(struct BST *root, char key[MAX])
{

    if (root == NULL)
        return root;

    int cmp_rezult=strcmp(root->data, key[MAX]);

    if (key[MAX] < root->data[MAX])
        root->left = deleteNode(root->left, key);
    else if (key[MAX] > root->data[MAX])
        root->right = deleteNode(root->right, key);

    else
    {
        if (root->left == NULL)//it the node has no children or only 1
        {
            struct BST *temp = root->right;
            free(root);
            return temp;
        }
        else if (root->right == NULL)
        {
            struct BST *temp = root->left;
            free(root);
            return temp;
        }

        //if the node have 2 kids
        struct BST *temp = minValueNode(root->right);

        //sorting
        root->data[MAX] = temp->data[MAX];

        //deleting
        root->right = deleteNode(root->right, temp->data[MAX]);
    }
    return root;
}


int main()
{
    char key[MAX];
    char ch;
    node *root=NULL,*temp;

    do
    {
        temp=create();
        if(root==NULL)
        {
            root=temp;
        }
        else
        {
            insert(root,temp);
        }
        printf("\nDo you want to enter more(y/n)?");
        ch=getch();

    }
    while(ch=='y'||ch=='Y');

    printf("\nPreorder Traversal: ");
    preorder(root);
    printf("\nWho shall we delete:");
    scanf("%s", &key[MAX]);
    deleteNode(root, key[MAX]);
    return 0;
}

node *create()
{
    node *temp;
    printf("\nEnter data:");

    temp=(node*)malloc(sizeof(node));

    fgets(&temp->data,MAX,stdin);

    temp->left=temp->right=NULL;
    temp->count=1;
    return temp;
}

void insert(node *root,node *temp)
{
    int cmp_rezult=strcmp(temp->data,root->data);
    if(cmp_rezult<0)
    {
        if(root->left!=NULL)
        insert(root->left,temp);
        else
        root->left=temp;
    }
    if(cmp_rezult>0)
    {
        if(root->right!=NULL)
        insert(root->right,temp);
        else
        root->right=temp;
    }
    if(cmp_rezult==0)
    {
        root->count++;
    }
}

void preorder(node *root)
{
    if(root!=NULL)
    {
        printf("\n%s Repeats:%d time(s)",root->data, root->count);
        preorder(root->left);
        preorder(root->right);
    }
}

这基本上与上面的代码相同,但是,在 BST *deleteNode 中,我更新了 cmp_result 中的代码,所以现在它写得正确了,之后我更新了 [=13] =] 语句在 cmp_result 下,我也更新了同一函数中的代码,从 root->data[MAX] = temp->data[MAX];strcpy(root->data,temp->data);,这是正确的做法

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX 15

typedef struct BST
{
    char data[MAX];
    struct BST *left;
    struct BST *right;
    int count;
} node;

node *create();
void insert(node *,node *);
void preorder(node *);
struct BST *minValueNode(struct BST *node)
{
    struct BST *current = node;


    while (current && current->left != NULL)
        current = current->left;

    return current;
}
struct BST *deleteNode(struct BST *root, char key[MAX])
{

    if (root == NULL)
        return root;

    int cmp_result = strcmp(key, root->data);

    if (cmp_result < 0)
        root->left = deleteNode(root->left, key);
    else if (cmp_result > 0)
        root->right = deleteNode(root->right, key);
    else
    {

        if (root->left == NULL)
        {
            struct BST *temp = root->right;
            free(root);
            return temp;
        }
        else if (root->right == NULL)
        {
            struct BST *temp = root->left;
            free(root);
            return temp;
        }

        
        struct BST *temp = minValueNode(root->right);

        strcpy(root->data,temp->data);

        
        root->right = deleteNode(root->right, temp->data);
    }
    return root;
}


int main()
{
    char key[MAX];
    char ch;
    node *root=NULL,*temp;

    do
    {
        temp=create();
        if(root==NULL)
        {
            root=temp;
        }
        else
        {
            insert(root,temp);
        }
        printf("\nDo you want to enter more(y/n)?");
        ch=getch();

    }
    while(ch=='y'||ch=='Y');

    printf("\nPreorder Traversal: ");
    preorder(root);
    printf("\n\nWho shall we delete:");
    fgets(key,MAX,stdin);
    printf("\n%s", key);
    deleteNode(root, key);
    printf("\nPreorder Traversal: ");
    preorder(root);
    return 0;
}

node *create()
{
    node *temp;
    printf("\nEnter data:");

    temp=(node*)malloc(sizeof(node));

    fgets(temp->data,MAX,stdin);

    temp->left=temp->right=NULL;
    temp->count=1;
    return temp;
}

void insert(node *root,node *temp)
{
    int cmp_rezult=strcmp(temp->data,root->data);
    if(cmp_rezult<0)
    {
        if(root->left!=NULL)
            insert(root->left,temp);
        else
            root->left=temp;
    }
    if(cmp_rezult>0)
    {
        if(root->right!=NULL)
            insert(root->right,temp);
        else
            root->right=temp;
    }
    if(cmp_rezult==0)
    {
        root->count++;
    }
}

void preorder(node *root)
{
    if(root!=NULL)
    {
        printf("\n%s Repeats:%d time(s)",root->data, root->count);
        preorder(root->left);
        preorder(root->right);
    }
}