C(二叉树为什么后面四个RE)

P3613 【深基15.例2】寄包柜

W121w @ 2023-07-03 22:30:16

#include <stdio.h>
#include <stdlib.h>
#define INC_SIZE 10
typedef struct stroage
{
    int cell;
    int tag;
    struct stroage *left;
    struct stroage *right;
} stroage;
stroage *root[100001] = {NULL};
int n, q;
void insert(int cabinet, int cell, int tag)
{
    stroage *r = (stroage *)malloc(sizeof(stroage));
    r->cell = cell;
    r->tag = tag;
    r->left = r->right = NULL;
    if (root[cabinet] == NULL)
    {
        root[cabinet] = r;
    }
    else
    {
        stroage *pre = NULL;
        stroage *q = root[cabinet];
        while (q)
        {
            if (q->cell > cell)
            {
                pre = q;
                q = q->left;
            }
            else if (q->cell < cell)
            {
                pre = q;
                q = q->right;
            }
            else if (q->cell == cell)
            {
                pre->tag = tag;
                free(r);
                return;
            }
        }
        if (pre->cell > cell)
        {
            pre->left = r;
        }
        else if (pre->cell < cell)
        {
            pre->right = r;
        }
    }
}
stroage *find(int cabinet, int cell)
{
    stroage *p = root[cabinet];
    while (p)
    {
        if (p->cell == cell)
            return p;
        else if (p->cell > cell)
            p = p->left;
        else
            p = p->right;
    }
    return NULL;
}
void delete_(stroage *p)
{
    if (!p)
        return;
    if (p->left)
    {
        delete_(p->left);
        p->left = NULL;
    }
    if (p->right)
    {
        delete_(p->right);
        p->right = NULL;
    }
    free(p);
}
int main()
{
    scanf("%d%d", &n, &q);
    for (int i = 0; i < q; i++)
    {
        int choice, cabinet, cell, tag;
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            scanf("%d%d%d", &cabinet, &cell, &tag);
            insert(cabinet, cell, tag);
            break;

        default:
            scanf("%d%d", &cabinet, &cell);
            stroage *temp = find(cabinet, cell);
            if(temp)
            printf("%d\n", temp->tag);
            break;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        delete_ (root[i]);
        root[i]=NULL;
    }
    system("pause");
    return 0;
}

|