入坑FHQ-Treap

· · 算法·理论

最近入坑了FHQ-Treap。

在开始平衡树之前,我们先了解一些基本的概念和平衡树的由来。

二叉搜索树(BST,Binary Search Tree),是一种二叉树,满足任意一个节点元的素都大于它的左子树的任意一个元素并且小于它的右子树的任意一个元素,可以实现插入、删除元素、获取元素排名、查找元素等功能,平均复杂度O(\log n),下面是一个二叉搜索树的例子 (我怎么这么懒啊连个图也不肯画) :

      100
      / \ 
     49 180 
     / /  \  
    3 150 190  

但是它有一个非常显著的缺点:假如一个特别毒瘤的出题人故意构造一个这样插入的数据:111 121 131 141 151,那么你的二叉搜索树就会变成这样:

111
 \ 
 121
   \
   131
     \
     141
       \
       151

看!它退化成了一条链!这样时间复杂度又是O(n)了!这可怎么办呢? 于是,平衡树诞生了。

平衡树也是一种二叉搜索树,但是它通过一些操作来保证尽量平衡,常见的平衡树有Treap、Splay、红黑树、笛卡尔树、AVL树、替罪羊树……这些平衡树大多通过旋转来修复失衡节点以保证时间复杂度。

但是,并不是所有的平衡树都是需要旋转的。FHQ-Treap就是,它的精妙之处在于它的分裂和合并操作。膜拜范浩强大仙!

在开始讲FHQ-treap之前我必须要先大致说一下treap是怎么回事。俗话说,treap=tree+heap,treap在加入节点的时候会额外分配一个优先级,值满足BST性质,优先级满足堆性质(这里我们按照小根堆来做,即一个节点的儿子的值大于这个节点的值)

需要存下来

  struct node {
          int value, size;
          //  ~~^~~  ~^~~
          //  元素值 子树大小
          unsigned priority;//优先值
          node *l, *r;
  }
    void pushup(node *p) {
        p->size = ((p->l) ? (p->l->size) : 0) + 1 + ((p->r) ? (p->r->size) : 0);
        //根据子节点的size更新p的size
    }

分裂与合并

这两个操作可以说是FHQ-Treap的最核心的操作。

按值分裂

这个操作传入V,可以把一棵树分裂成两个部分,一个部分所有元素都\leq V,另一个部分所有元素都>V。
假如我们要分裂以root为根的树,分裂成\leq V的和>V的,我们看一下root的元素的值是否\leq V,如果是,那么它在左边部分,那右边部分的所有节点都在root的右子树当中,直接递归。
如果root的元素的值>V,那么在左子树中递归分裂,分裂出来的>V的部分作为>V的部分的左子树。 比如

      100
      / \ 
     49 180 
     / /  \  
    3 150 190  
``` 100 / | 49 | 180 / | \ 3 150 190 ``` ::::success[code] ```cpp node* split(node *&x, int value) { //x是要分裂的树,value就是上面的V if (!x)return nullptr;//别忘了空指针 if (x->value <= value) { node* tmp = split(x->r, value); if (x)pushup(x); if (tmp)pushup(tmp);//别忘了pushup return tmp; } node *y = x; x = x->l; if (x)y->l = split(x, value); else y->l = nullptr; if (x)pushup(x); if (y)pushup(y); return y; } ``` :::: 时间复杂度$O(\log n)$。 ### 按排名分裂 这个操作接受$R$,然后把这棵树分成两部分,第一部分正好有$R$个节点。 这个懒得讲了。 ```cpp node* split(node *&root, int rank) { //这个rank就是R if (!pushdown(root))return nullptr; int lsize = (root->l) ? (root->l->size) : 0; if (lsize + 1 == rank) {//右子树正好是第二部分 node *tmp = root->r; root->r = nullptr; pushup(root), pushup(tmp); return tmp; } else if (rank <= lsize) {//第一部分全在左子树 node *tmp = root; root = root->l; tmp->l = split(root, rank); pushup(root), pushup(tmp); return tmp; } else {//第二部分全在右子树 node *tmp = split(root->r, rank - lsize - 1); pushup(root), pushup(tmp); return tmp; } } ``` 把$V=160$换成$R=4$就是例子。 时间复杂度$O(\log n)$。 ### 合并 合并不分两种了,合并就一种。 接受两棵树,第一棵树所有元素都小于第二棵树的所有元素,并且都满足treap性质,合并成一颗满足treap性质的树。 根据优先级决定是把x合并到y的左子树还是把y合并到x的右子树。 :::success[code] ```cpp node* merge(node *x, node *y) { if (!x || !y)return x ? x : y; if (x->priority < y->priority) { y->l = merge(x, y->l); if (y)pushup(y); return y; } else { x->r = merge(x->r, y); if (x)pushup(x); return x; } } ``` ::: 时间复杂度$O(\log n)$。 ## 基本操作 ~~这些操作时间复杂度全是$O(\log n)$~~ ### 插入 把它按value分裂成x,y,把x与新节点合并,再与y合并,两行完事。 ### 删除 把它按value分裂成x,z,再按value-1分裂成x,y,y里面肯定都是值为value,合并y的左子树和右子树,再与x合并,再与z合并,好麻烦。 ### 获取排名 按value-1分开后,第一部分的大小+1就是排名,最后别忘了合并。 ### 根据排名获取元素 假如当前在root下面,要获取第$k$个元素,左子树大小是$X$。 如果$X\geq k$,那么在左子树递归; 如果$X+1=k$,那么就是答案 ~~(废话)~~ ; 如果$X+1<k$,那么递归右子树,$k=k-x-1$。 ### 前驱/后继 前驱是按value-1分裂后第一部分最右边的部分就是,最后在合并。 后继是按value分裂后第二部分的最左边就是,还是最后再合并。 ### 完整[代码](https://www.luogu.com.cn/record/292077769) :::success[code]{open} ```cpp line-numbers #include<bits/stdc++.h> using namespace std; int n, opt, x; namespace treap { int cnt; struct node { int value, size; unsigned priority; node *l, *r; }nodes[100005]; node* newnode(int value, unsigned priority) { return &(nodes[++cnt] = { value,1,priority,nullptr,nullptr }); } void pushup(node *p) { p->size = ((p->l) ? (p->l->size) : 0) + 1 + ((p->r) ? (p->r->size) : 0); } node* split(node *&x, int value) { if (!x)return nullptr; if (x->value <= value) { node* tmp = split(x->r, value); if (x)pushup(x); if (tmp)pushup(tmp); return tmp; } node *y = x; x = x->l; if (x)y->l = split(x, value); else y->l = nullptr; if (x)pushup(x); if (y)pushup(y); return y; } node* merge(node *x, node *y) { if (!x || !y)return x ? x : y; if (x->value > y->value)puts("bad!"); if (x->priority < y->priority) { y->l = merge(x, y->l); if (y)pushup(y); return y; } else { x->r = merge(x->r, y); if (x)pushup(x); return x; } } node* insert(node *root, int value, unsigned priority) { node *tmp = split(root, value); return merge(merge(root, newnode(value, priority)), tmp); } node* remove(node *root, int value) { node *b = split(root, value), *a = split(root, value - 1); return merge(merge(root, (a ? merge(a->l, a->r) : nullptr)), b); } int getrank(node *&root, int value) { node *tmp = split(root, value - 1); int ans = (root ? (root->size) : 0) + 1; root = merge(root, tmp); return ans; } node* getbyrank(node *root, int rank) { while (root) { int lsize = 0; if (root->l)lsize = root->l->size; if (lsize + 1 == rank)return root; if (rank <= lsize)root = root->l; else root = root->r, rank = rank - lsize - 1; } return nullptr; } int getprev(node *&root, int value) { node *tmp = split(root, value - 1); node *now = root; while (now->r) now = now->r; int ans = now->value; root = merge(root, tmp); return ans; } int getnext(node *&root, int value) { node *tmp = split(root, value); node *now = tmp; while (now->l) now = now->l; int ans = now->value; root = merge(root, tmp); return ans; } } int main() { treap::node *root = nullptr; scanf("%d", &n); mt19937 rand((unsigned)n * n); while (n--) { scanf("%d%d", &opt, &x); switch (opt) { case 1: root = treap::insert(root, x, rand()); break; case 2: root = treap::remove(root, x); break; case 3: printf("%d\n", treap::getrank(root, x)); break; case 4: printf("%d\n", treap::getbyrank(root, x)->value); break; case 5: printf("%d\n", treap::getprev(root, x)); break; case 6: printf("%d\n", treap::getnext(root, x)); break; } } return 0; } ``` :::