`
Coco_young
  • 浏览: 127534 次
  • 性别: Icon_minigender_1
  • 来自: 湖南长沙
社区版块
存档分类
最新评论

SplayTree实现_简化版

 
阅读更多

(1)

template<class DataType>
class SplayTree{
#define MAXN 1000010
private:
    int ch[MAXN][2],pre[MAXN],pool[MAXN];
    DataType key[MAXN];
    int top,root,tot;
    int malloc(DataType dt){
        int x;
        if(top!=0) x = pool[--top];
        else x = ++tot;
        key[x] = dt;
        return x;
    }
    void free(int x){
        ch[x][0] = ch[x][1] = pre[x] = 0;
        pool[top++] = x;
    }
    void rotate(int x,int f){//f == 0 为zag f == 1 为zig
        int y = pre[x]; int z = pre[y];
        ch[y][!f] = ch[x][f];
        if(ch[y][!f]) pre[ch[y][!f]] = y;
        ch[x][f] = y;
        pre[y] = x;
        if(z) ch[z][0]==y?ch[z][0] = x:ch[z][1] = x;
        pre[x] = z;
    }
    void splay(int x,int &rt){
        int y,z;
        while(pre[x]){
            y = pre[x]; z = pre[y];
            if(!z){
                rotate(x,ch[y][0]==x);
            }else{
                int f = ch[z][0]==y;
                if(ch[y][!f]==x){
                    rotate(y,f); rotate(x,f);
                }else{
                    rotate(x,!f); rotate(x,f);
                }
            }
        }
        rt = x;
    }
    int find_help(DataType k,int rt){
        if(!rt) return 0;
        if(k==key[rt])return rt;
        return k<key[rt]?find_help(k,ch[rt][0]):find_help(k,ch[rt][1]);
    }
    int findmax_help(int rt){
        int x = rt;
        while(x&&ch[x][1]) x = ch[x][1];
        return x;
    }
    int findmin_help(int rt){
        int x = rt;
        while(x&&ch[x][0]) x = ch[x][0];
        return x;
    }
    int join(int rt1,int rt2){
        if(!rt1) return rt2;
        if(!rt2) return rt1;
        int x = findmax_help(rt1);
        splay(x,rt1);
        ch[rt1][1] = rt2;
        pre[rt2] = rt1;
        return rt1;
    }
    int split(DataType k,int &rt,int &rt1,int &rt2){
        int x = find_help(k,rt);
        if(!x) return 0;
        splay(x,rt);
        rt1 = ch[rt][0]?ch[rt][0]:0;
        pre[rt1] = 0;
        rt2 = ch[rt][1]?ch[rt][1]:0;
        pre[rt2] = 0;
        return rt;
    }
    int insert_help(DataType k,int &rt,int father){
        if(!rt){
            rt = malloc(k);
            pre[rt] = father;
            return rt;
        }
        return insert_help(k,ch[rt][!(k<key[rt])],rt);
    }
public:
    void insert(DataType k){
        int x = insert_help(k,root,0);
        splay(x,root);
    }
    void remove(DataType k){
        int rt1,rt2;
        int x = split(k,root,rt1,rt2);
        if(!x) return;
        free(x);
        root = join(rt1,rt2);
    }
    int findmax(DataType &k){
        int x = findmax_help(root);
        if(x) splay(x,root);
        k = key[x];
        return x;
    }
    int findmin(DataType &k){
        int x = findmin_help(root);
        if(x) splay(x,root);
        k = key[x];
        return x;
    }
};

(2)

template<class DataType>
class SplayTree{
#define MAXN 1000010
private:
    int ch[MAXN][2],pre[MAXN],pool[MAXN];
    DataType key[MAXN];
    int top,root,tot;
    int malloc(DataType dt){
        int x;
        if(top!=0) x = pool[--top];
        else x = ++tot;
        key[x] = dt;
        return x;
    }
    void free(int x){
        ch[x][0] = ch[x][1] = pre[x] = 0;
        pool[top++] = x;
    }
    void rotate(int x,int f){//f == 0 为zag f == 1 为zig
        int y = pre[x]; int z = pre[y];
        ch[y][!f] = ch[x][f];
        if(ch[y][!f]) pre[ch[y][!f]] = y;
        ch[x][f] = y;
        pre[y] = x;
        if(z) ch[z][0]==y?ch[z][0] = x:ch[z][1] = x;
        pre[x] = z;
    }
    void splay(int x,int &rt){
        int y,z;
        while(pre[x]){
            y = pre[x]; z = pre[y];
            if(!z){
                rotate(x,ch[y][0]==x);
            }else{
                int f = ch[z][0]==y;
                if(ch[y][!f]==x){
                    rotate(y,f); rotate(x,f);
                }else{
                    rotate(x,!f); rotate(x,f);
                }
            }
        }
        rt = x;
    }
    int find_help(DataType k,int rt){
        if(!rt) return 0;
        if(k==key[rt])return rt;
        return k<key[rt]?find_help(k,ch[rt][0]):find_help(k,ch[rt][1]);
    }
    int findmax_help(int rt){
        int x = rt;
        while(x&&ch[x][1]) x = ch[x][1];
        return x;
    }
    int findmin_help(int rt){
        int x = rt;
        while(x&&ch[x][0]) x = ch[x][0];
        return x;
    }
    int insert_help(DataType k,int &rt,int father){
        if(!rt){
            rt = malloc(k);
            pre[rt] = father;
            return rt;
        }
        return insert_help(k,ch[rt][!(k<key[rt])],rt);
    }
    void remove_help(DataType k,int &rt,int father){
        if(!rt) return;
        if(k==key[rt]){
            if(ch[rt][0]==0||ch[rt][1]==0){
                int x = rt;
                rt = ch[rt][0]+ch[rt][1];
                if(rt){ pre[rt] = father; splay(rt,root); }
                free(x);
                return;
            }
            int x = findmin_help(ch[rt][1]);
            key[rt] = key[x];
            remove_help(key[rt],ch[rt][1],rt);
            splay(rt,root);
        }
        remove_help(k,ch[rt][!(k<key[rt])],rt);
    }
public:
    void insert(DataType k){
        int x = insert_help(k,root,0);
        splay(x,root);
    }
    void remove(DataType k){
        remove_help(k,root,0);
    }
    int findmax(DataType &k){
        int x = findmax_help(root);
        if(x) splay(x,root);
        k = key[x];
        return x;
    }
    int findmin(DataType &k){
        int x = findmin_help(root);
        if(x) splay(x,root);
        k = key[x];
        return x;
    }
};


分享到:
评论

相关推荐

    SplayTree优化版C源代码

    ### SplayTree优化版C源代码解析 #### 一、简介 Splay Tree(伸展树)是一种自平衡的二叉查找树,在实际应用中表现出很高的效率。与传统的AVL Tree相比,Splay Tree在大部分情况下能提供更快的查询速度。本文档提供...

    C++ STL PBDS库讲解

    例如,用户可以选择不同的底层实现,如红黑树(rb_tree_tag)、伸展树(splay_tree_tag)或有序向量树(ov_tree_tag)。通常红黑树提供了较好的平衡性能,所以在总体性能上,使用rb_tree_tag可能更优。 下面是一个...

    QTREE 解法的一些研究 信息学

    本文关注的是简化版的动态树问题,即仅包含树形态的操作和对某个节点到根节点路径的操作。此类问题的解决策略往往涉及到高级数据结构的应用,如Link-Cut Trees等。 #### Link-Cut Trees **Link-Cut Trees** 是一种...

    多种压缩算法的源代码

    - **SPLAY**:可能与自平衡二叉搜索树(如Splay Tree)有关,用于快速查找和更新字典中的条目。 - **AR002**:可能是一个特定版本的归档格式或压缩算法。 - **ASH**:可能代表Adaptive Shingling Hashing,一种...

    高级数据结构-----刘汝佳

    **树状数组**(Binary Indexed Tree, BIT)是一种简化版的线段树,用于快速求解区间和等操作。树状数组的主要优点在于其实现简单,空间占用少。 #### RMQ与LCA **RMQ(Range Minimum Query)**是区间最小值查询问题,...

    2019清华计算机912综合回忆版.pdf

    - 伸展树(Splay Tree):伸展树是一种自调整的二叉查找树,如果访问遵循局部性原理,它的平均操作时间复杂度可以达到O(logn),否则可能不是O(logn)。 - KMP算法:KMP算法在不使用改进的next[]数组时,仍然可以...

    可持久化数据结构研究

    可持久化数据结构是计算机科学中一种高级的数据结构,其核心特性是在对数据结构进行修改操作后,能够保留操作之前的版本,从而实现时间版本的并行管理。这种数据结构特别适合那些需要频繁回溯历史状态的应用场景,...

    acm-book.pdf

    树状数组是一种支持在一维数组上实现单点更新和前缀和查询操作的数据结构,特别适用于解决区间更新和区间查询问题。 #### 4.3 线段树 线段树是一种能够高效处理区间查询和更新操作的数据结构,常用于解决区间最大/...

    信息学奥赛-省选及NOI课程表(2020.08.31).pdf

    5. **动态树(dsu on tree)**:一种处理树上动态变化问题的数据结构。 #### 三、数论进阶 1. **欧拉函数**:表示小于等于某个正整数且与该数互质的正整数的数量。 2. **莫比乌斯反演**:一种数论变换,用于将复杂...

Global site tag (gtag.js) - Google Analytics