fhq balanced tree treap study notes (the non-teaching)

fhq balanced tree treap study notes (the non-teaching)

(Non-full version)

Open a balanced tree to study the pros and cons of some of the balance between trees, decided to learn fhq treap, splay trees and scapegoat? (I heard fhq treap and the scapegoat is better to write? Splay slower but more useful?), The remaining balance of trees, like red-black tree, sbtree, avl on the other must learn when to learn to

Get started now fhq treap, muttered behind two first

What 1.fhqtreap that?

My personal understanding is treap simplified version, you can maintain a sequence of many useful things, such as precursor, suffix, rankings, etc., through two major operations merge and split balance balanced tree

2.fhqtreap how to write?

See example

3.fhqtreap how to use?

I am also not clear, I did a title template title

Recommended topics

t1

[Template] general balanced tree

Too templates, first put the code (not the friendly pressure line version)

//I love Nanami Chiaki
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
struct fhqTreap{
    int val,rnd,sz,son[2];
}treap[maxn];
int root=0,tot=1;
inline int newnode(int v){
    int nw=tot++;
    treap[nw].rnd=rand();treap[nw].val=v;treap[nw].sz=1;
    return nw;
}
inline void update(int nw){
    treap[nw].sz=treap[treap[nw].son[0]].sz+treap[treap[nw].son[1]].sz+1;
}
void splitval(int nw,int k,int &x,int &y){
    if (!nw){x=0;y=0;return;}
    if (treap[nw].val<=k){
        x=nw;splitval(treap[nw].son[1],k,treap[nw].son[1],y);
    }
    else{
        y=nw;splitval(treap[nw].son[0],k,x,treap[nw].son[0]);
    }
    update(nw);
}
int merge(int x,int y){
    if (!x || !y) return x^y;
    int re;
    if (treap[x].rnd<treap[y].rnd){
        re=x;treap[x].son[1]=merge(treap[x].son[1],y);
    }
    else{
        re=y;treap[y].son[0]=merge(x,treap[y].son[0]);
    }
    update(re);return re;
}
int kth(int nw,int k){
    if (k==treap[treap[nw].son[0]].sz+1) return nw;
    if (k<=treap[treap[nw].son[0]].sz) return kth(treap[nw].son[0],k);
    return kth(treap[nw].son[1],k-treap[treap[nw].son[0]].sz-1);
}
int main(){
    srand(time(0));
    int n;scanf("%d",&n);
    for (int i=0;i<n;i++){
        int op,v;
        scanf("%d%d",&op,&v);
        if (op==1){
            int x,y;
            splitval(root,v,x,y);
            root=merge(merge(x,newnode(v)),y);
        }
        else if(op==2){
            int x,y,z;
            splitval(root,v,x,z);
            splitval(x,v-1,x,y);
            y=merge(treap[y].son[0],treap[y].son[1]);
            root=merge(merge(x,y),z);
        }
        else if(op==3){
            int x,y;
            splitval(root,v-1,x,y);
            printf("%d\n",treap[x].sz+1);
            root=merge(x,y);
        }
        else if(op==4){
            int nw=kth(root,v);
            printf("%d\n",treap[nw].val);
        }
        else if(op==5){
            int x,y;
            splitval(root,v-1,x,y);
            printf("%d\n",treap[kth(x,treap[x].sz)].val);
            merge(x,y);
        }
        else{
            int x,y;
            splitval(root,v,x,y);
            printf("%d\n",treap[kth(y,1)].val);
            merge(x,y);
        }
    }
    return 0;
} 

It is not clear that amazing?

Nonsense, not

Talk about what split

Role is to split a tree split into two trees, two trees meet certain relationship? Probably all numbers in all of x is less than the number in y

Has two distributions, one is by ranking points, one is by val points, almost, that is, if the value of the root node The tree will be assigned to our tree on the left, put the root become our division root of the tree on the left and right subtree continue dividing, dividing up the left sub-tree, to the right sub-tree root node of the current meet bst nature, segmentation is out of the right subtree right subtree friends then layer by layer returns, the resulting x and y is, we need two trees, and a little difficult to understand for iteration I think the problem for a long time iteration, but in fact is certainly no problem, the x and y here do not understand the root into two trees Finally, we need to understand two trees and roots, no problem required for the upper cycle.

The role of merge is relatively simple, take a look at the program understand? Konjac is split mainly I do not understand. But then merge There is a point worth noting is that, when I began to learn where he just did not find the conditions to maintain the balance in the last years found that only merge uses rnd understood why. The merge process is equivalent to the balance of the process? I have not been able to fully understand the form of the tree, if the latter topic I communicated the words will later say

The other is easier to understand

(Unfinished, to be more)

Guess you like

Origin www.cnblogs.com/xxjAc/p/12120282.html