Splay

                                                      Splay

     写这玩意好累~~~~

     这里贴一份用指针写的(为什么周围的神犇不喜欢用指针呢??)

   

#include<cstdio>
#include<iostream>
#include<cstring>
#define MAXN  100000


using namespace std;


class SplayTree{
private:
  struct node
      {
         int key;
         node *lc,*rc,*fa;
      };
      node *root,*tail;
      node pool[MAXN];
  inline void rotate(node *x)
  {
      node *y=x->fa;
      node *z=y->fa;
      node *b=x==y->lc ? x->rc : x->lc;
      x->fa=z,y->fa=x;
      if(b)
        b->fa=y;
      if(z)
        ( z->lc==y ? z->lc : z->rc )=x;
      if(x==y->lc)
        x->rc=y,y->lc=b;
      else
        x->lc=y,y->rc=b;
  }
  inline void Splay(node *x,node *target=NULL)
  {
     while(x->fa!=target)
     {
          if(x->fa->fa!=target)
          {
                  if((x->fa->fa->lc==x->fa)==(x->fa->lc==x))
                    rotate(x->fa);
                  else
                    rotate(x);
          }
          rotate(x);
     }
     if(target==NULL)
        root=x;
  }
  inline node *find(int key)
  {
      node *p=root;
      while(p)
      {
         if(key>p->key)
            p=p->rc;
         else
            if(key<p->key)
            p=p->lc;
         else
            return p;
      }
      return NULL;
  }
public:
    SplayTree()
    {
               root=NULL;
               tail=pool;
    }
    inline bool  Find(const int &key)
    {
        node *p=find(key);
        if(!p)
            return false;
        Splay(p);
        return  true;
    }
    inline void Insert(const int key)
    {
        node **q=&root;
        node *p=root;
        node *fp=NULL;
        while(p)
        {
             fp=p;
             if(key>p->key)
                q=&p->rc,p=p->rc;
             else
             if(key<p->key)
               q=&p->lc,p=p->lc;
             else
             {
                 Splay(p);
                 return ;
             }
        }
        p = tail++;
        p->key = key;
        p->fa = fp, p->lc = p->rc = NULL;
        *q = p;
        Splay(p);
    }
    inline void Delete(const int key)
    {
         node *p=find(key);
         Splay(p);
         if(!p->lc&&!p->rc)
         root=NULL;
         else
            if(!p->lc)
            root->fa=NULL,root=p->rc;
            else
                if(!p->rc)
                root->fa=NULL,root=p->lc;
            else
            {
                node *pre=p->lc,*next=p->rc;
                while(pre->rc)
                    pre=pre->rc;
                while(next->lc)
                    next=next->lc;
                Splay(pre);
                Splay(next,pre);
                next->lc=NULL;
            }
    }
};

         这也有个Splay专辑~~~~~很有用呢(用数组写的)

http://www.cnblogs.com/proverbs/archive/2013/01/15/2860717.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值