求助!测了几组数据感觉全是对的,全RE了,不知道为什么T^T
查看原帖
求助!测了几组数据感觉全是对的,全RE了,不知道为什么T^T
476635
Skye_rs楼主2021/6/25 21:15
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 105
#define INF 2147483647
  
typedef struct binaryTree
{
    int val;
    int Lchildnum;
    int Rchildnum;
    struct binaryTree *left, *right;
} binaryTree;
binaryTree *head=NULL;
int flag;

binaryTree *insert(binaryTree *cur, int x);
binaryTree *createRank(binaryTree *cur);
int getPos(binaryTree *cur, int x);
int checkPos(binaryTree *cur, int x);
int getFather(binaryTree *cur, int x, int ans);
int getLchild(binaryTree *cur, int x, int ans);

int main()
{
    int opnum, op, x;

    scanf("%d", &opnum);
    while (opnum--)
    {
        scanf("%d%d", &op, &x);
        switch (op)
        {
        case 1: //查询x的排名
            printf("%d\n", getPos(head, x) + 1);
            break;
        case 2: //查询排名为x的数
            printf("%d\n", checkPos(head, x));
            break;
        case 3: //查询x前驱
            printf("%d\n", getFather(head, x, -INF));
            break;
        case 4: //查询x的后继
            printf("%d\n", getLchild(head, x, INF));
            break;
        case 5:
            flag = 1;
            head = insert(head, x);
            break;
        default:
            break;
        }
    }
    return 0;
}
binaryTree *insert(binaryTree *cur, int x)
{
    if (cur == NULL)
    {
        cur = (binaryTree *)malloc(sizeof(binaryTree));
        cur->val = x;
        cur->left = cur->right = NULL;
        cur->Lchildnum = cur->Rchildnum = 0;
        return cur;
    }
    if (x > cur->val)
    {
        cur->right=insert(cur->right, x);
        if (flag)
        {
            cur->Rchildnum++;
        }
    }
    else if (x < cur->val)
    {
        cur->left = insert(cur->left, x);
        if (flag)
        {
            cur->Lchildnum++;
        }
    }
    else if (x == cur->val)
    {
        flag = 0;
        return cur;
    }
    return cur;
}
int getPos(binaryTree *cur, int x)
{
    if (cur->val == x)
        return cur->Lchildnum;
    if (cur->val > x)
        return getPos(cur->left, x);
    else
        return getPos(cur->right, x) + cur->Lchildnum + 1;
}
int checkPos(binaryTree *cur, int x)
{
    if (cur->Lchildnum == x - 1)
        return cur->val;
    if (cur->Lchildnum > x - 1)
        return checkPos(cur->left, x);
    else
        return checkPos(cur->right, x - 1 - cur->Lchildnum);
}

int getFather(binaryTree *cur, int x, int ans)
{
    if (cur->val >= x)
    {
        if (!cur->left)
            return ans;
        else
            return getFather(cur->left, x, ans);
    }
    else
    {
        if (!cur->right)
            return cur->val;
        return getFather(cur->right, x, cur->val);
    }
}
int getLchild(binaryTree *cur, int x, int ans)
{
    if (cur->val <= x)
    {
        if (!cur->right)
            return ans;
        else
            return getLchild(cur->right, x, ans);
    }
    else
    {
        if (!cur->left)
            return cur->val;
        return getLchild(cur->left, x, cur->val);
    }
}
2021/6/25 21:15
加载中...