繁体   English   中英

C 语言:从字符串的 BST 中删除节点

[英]C language:Removing a node from a BST of strings

我正在尝试编写一个 function,它将从 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的代码,所以现在它已经正确编写了,之后我更新了cmp_result下的if语句,我也更新了代码同样的 function,从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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM