伸展树--郁闷的出纳员。。WA

#include <stdlib.h>
#include <stdio.h>
 int size,k;
typedef struct tree_node Tree;
 struct tree_node
{
    Tree * left, * right;
    int item;
};
int count=0,min;
Tree * splay (int i, Tree * t)
{
    Tree N, *l, *r, *y;
    if (t == NULL)
        return t;
    N.left = N.right = NULL;
    l = r = &N;
    while(1)
    {
        if (i < t->item)
        {
            if (t->left == NULL)
            {
                break;
            }
            if (i < t->left->item)
            {
                y = t->left;
                t->left = y->right;
                y->right = t;
                t = y;
                if (t->left == NULL)
                {
                    break;
                }
            }
            r->left = t;
            r = t;
            t = t->left;
        }     
        else if (i > t->item)
        {    
            if (t->right == NULL)
            {
                break;
            }
            if (i > t->right->item)
            {
                y = t->right;
                t->right = y->left;
                y->left = t;
                t = y;
                if (t->right == NULL)
                {
                    break;
                }
            }
            l->right = t;
            l = t;
            t = t->right;
        }     
        else    
        {
            break;
        }
    }
    l->right = t->left;
    r->left = t->right;
    t->left = N.right;
    t->right = N.left;
    return t;
}

Tree * insert(int i, Tree * t)
{
    Tree *n;
    
    n = (Tree *) malloc (sizeof (Tree));
    n->item = i;
    if (t == NULL)
    {
        n->left = n->right = NULL;
        size = 1;
        return n;
    }
    t = splay(i,t);
    if (i < t->item)
    {
        n->left = t->left;
        n->right = t;
        t->left = NULL;
        size ++;
        return n;
    }
    else if (i >= t->item)
    {
        n->right = t->right;
        n->left = t;
        t->right = NULL;
        size++;
        return n;
    }
    else
    {
        free(n);
        return t;
    }
}

Tree * Delete(int i, Tree * t)
{
    Tree * x;
    if (t==NULL)
    {
        return NULL;
    }
    t = splay(i,t);
    if (i == t->item)
    {            
        if (t->left == NULL)
        {
            x = t->right;
        }
        else
        {
            x = splay(i, t->left);
            x->right = t->right;
        }
        size--;
        count++;
        free(t);
        return x;
    }
    return t;                   
}

void search(Tree *t)
{
     if(t)
     {
          if(k==0)return ;
          search(t->right);
          if(k==1)
          printf("%d\n",t->item);
          k--;
          search(t->left);
     }
}


int yanzheng(Tree *l)
{
     while(l)
     {
        if(l->item<min)
        return l->item;
        else if(min<=l->item)
         l=l->left;
     }
     return min;
}

Tree *adjust(Tree *pnode)
{
    int s;
    s=yanzheng(pnode);
    while(s<min)
    {
       pnode=Delete(s,pnode);
       s=yanzheng(pnode);
    }
    return pnode;
}

int prea(Tree *pnode)
{
    if(pnode)
    {
       prea(pnode->left);
       pnode->item+=k;
       prea(pnode->right);
    }
}

void pres(Tree *pnode)
{
    if(pnode)
    {   pnode->item-=k;
        pres(pnode->left);
        pres(pnode->right);
    }
}

int main()
{
    Tree * root;
    char s,c[10];
    int i,n;
    scanf("%d%d",&n,&min);
        root = NULL;             
        size = count = 0;
        while(n--)
            {
              gets(c);
              scanf("%c %d",&s,&k);
              if(s=='I')
              {
                 if(k<min)
                  {
                     continue;
                  }
                  else root=insert(k,root);
              }
              else if(s=='A')
                    prea(root);
              else if(s=='S')
                    {
                     pres(root);
                     root=adjust(root);
                    }
              else if(s=='F')
                  if(k>size)
                    printf("-1\n");
                  else search(root);
            }
        printf("%d\n",count);
    
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/huzhenbo113/articles/3219088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值