Persistence can be balanced tree (fhq treap)

Excerpt: https://www.cnblogs.com/mjtcn/p/8028926.html% Great God 

text

Finishing the FHQ treap

 

treap = tree + heap, i.e., while satisfying properties binary search tree and the heap.

In order to ensure that both sides of the tree the size of the balance as much as possible, so there is a key value, so that he meet piled nature, to maintain the balance of the tree, key value is random.

 

treap balanced tree has a general function of the precursor, the successor, k-th largest, ranking query, insert, delete. Also better to write

But for problems interval can not be done, for example,

  • Decrease interval
  • Interval most value
  • Interval reversal (reverse)
  • Moving section (the section cut and paste)

(Splay that can be done)

However, there is a magic data structure, i.e., meet treap function can also operate on the interval - FHQ treap

 

FHQ treap only two basic operations, so the amount of code is also much smaller.

split

Separated into two speak a treap treap.

There are two types of separation, in accordance with a weight val points, into a k less than or equal, greater than one. Another is the number of k on the front section removed.

Weight:

For a Treap, less than or equal k points are present in a subtree, but may have greater than Zheke subtree of k, so that when the split is to reconstruct the tree.

 

Copy the code
 1 void Split(int now,int k,int &x,int &y) {
 2     if (!now) x = y = 0;
 3     else {
 4         if (val[now] <= k) 
 5             x = now,Split(ch[now][1],k,ch[now][1],y);
 6         else 
 7             y = now,Split(ch[now][0],k,x,ch[now][0]);
 8         pushup(now);
 9     }
10 }
Copy the code

The code is very wonderful, it refers to the two values, x, y, these two values ​​is the most important of the two variables reconstruction, must be taken address character.

x is a reference node k or less (assuming a), the right son, y is greater than a reference node is left son k.

Where a is less than or equal to k, its left subtree is less than or equal to k, the right son, but is not necessarily less than k, so here it is taken right son, when encountering the first node is less than k, let it be a right son.

FIG follows, k = 6, then a is less than 6, and go to the right, the right son is found to be greater than 6, so that a right son is to change, the next process to go left, the right son of a point to point to a weight of 6.

Reconstruction process: If the value is less than the current point k now that he's left some are less than k, so go to the right.

Complexity of  O ( L O G n- ) O (logN)

 

The front section number k

 

Copy the code
 1 void Split(int now,int k,int &x,int &y) {
 2     if (!now) x=y=0;
 3     else {
 4         if (k <= siz[ch[now][0]])
 5             y = now,Split(ch[now][0],k,x,ch[now][0]);
 6         else
 7             x = now,Split(ch[now][1],k-siz[ch[now][0]]-1,ch[now][1],y);
 8         pushup(now);
 9     }
10 }
Copy the code

 

 

The principle is the same, not explained in detail.

Complexity, O ( L O G n- ) O (logN)

 

merge

The combined two subtree to ensure that all points of the weights of all the tree nodes are smaller than particles of the second subtree.

Then rebuild as long as the nature of the heap enough.

There are two variables x, y,

 

Copy the code
 1 int Merge(int x,int y) {
 2     if (!x || !y) return x + y;
 3     if (key[x] < key[y]) {
 4         ch[x][1] = Merge(ch[x][1],y);
 5         pushup(x); return x;    
 6     }
 7     else {
 8         ch[y][0] = Merge(x,ch[y][0]);
 9         pushup(y); return y;
10     }
11 }
Copy the code

 

Here you will find that when x tree key hours, only the left half of x added to the reconstruction of the tree, y subtree hours, it only added to the right half of the sub-tree. In order to meet treap of nature.

 

Complexity of  O ( L O G n- ) O (logN)

 

Two basic operation is complete.

 

insert

Inserting a number of weight value k.

Process: The treap into two, less than or equal to k, k is greater than, x, and the two sub-trees can be combined

 

Split(Root,k,x,y);
Root = Merge(Merge(x,makenode(k)),y);

 

delete

The right to delete a number value of k.

Process: first divided into k less than or equal to a greater than k, b, then x into the k-1 or less c and d is greater than k-1, k is d, it will merge two sons d, and then c, b can be combined;

 

Split (Root, a, x, y); 
Split (x-1, x, z); 
z = Merge (CH [i] [0], b [i] [1]); 
Root = Merge (Merge (x, z), s);

 

k ranking

K is seeking rankings

Procedure: into a k-1 x less than or equal to and greater than k-1 y two subtrees, x is the size of the sub-tree of rank k.

 

Split(Root,k-1,x,y);
printf("%d\n",siz[x]+1);
Root = Merge(x,y);

 

The first number k

Seeking the number of k

Process: the determination and splay, treap like;

Copy the code
inline int getkth(int p,int k) {
    while (true) {
        if (k == siz[ch[p][0]] + 1) return p;
        if (ch[p][0] && k <= siz[ch[p][0]]) p = ch[p][0];
        else k-= ((ch[p][0] ? siz[ch[p][0]] : 0) + 1),p = ch[p][1];
    }
}
Copy the code

 

Precursor

K is seeking rankings

Procedure: into a k-1 x less than or equal to and greater than k-1 y two subtrees, x subtree is the largest number x precursor.

Split(Root,k-1,x,y);
printf("%d\n",val[getkth(x,siz[x])]);
Root = Merge(x,y);

 

Successor

K is seeking rankings

Process: k less into the x, and y k is greater than two subtrees subtree y x is the smallest number of precursors.

Split(Root,k,x,y);
printf("%d\n",val[getkth(y,1)]);
Root = Merge(x,y);

 

The basic operation is that these are the FHQtreap

Guess you like

Origin www.cnblogs.com/ZJXXCN/p/11455088.html