Treap with implicit keys I implemented an implicit treap and tested it a little. It works fine but I'd like to know if there are ways to improve/simplify things and/or if some parts are incorrect. Later, I'll augment code to allow for more operations (interval reversal, shifts, etc..).

Treap node.

struct Node
{   int v, p, c; /* value, priority, subtree size */
    Node *l, *r; /* left and right subtrees */

    Node(int val, int priority = rand())
    {   v = val;
        p = priority;
        l = r = 0;
    }
};


Size of subtree rooted at node x.

int count(Node *x)
{   return x ? x-&gt;c : 0;
}


Merge.

void join(Node **x, Node *left, Node *right)
{   if(left == 0 || right == 0)
        *x = left ? left : right;
    else
    {   if(left-&gt;p &gt; right-&gt;p)
        {   join(&amp;left-&gt;r, left-&gt;r, right);
            *x = left;
        }
        else
        {   join(&amp;right-&gt;l, left, right-&gt;l);
            *x = right;
        }
        (*x)-&gt;c = count((*x)-&gt;l) + count((*x)-&gt;r) + 1;
    }
}


Split.

void split(Node *x, int k, Node ** left, Node **right)
{   Node *tmp;
    if(x == 0) *left = *right = 0;
    else
    {   int c = count(x) - count(x-&gt;r);
        if(k &lt; c)
        {   split(x-&gt;l, k, left, &amp;tmp);
            *right = x, (*right)-&gt;l = tmp;
        }
        else
        {   split(x-&gt;r, k - c, &amp;tmp, right);
            *left = x, (*left)-&gt;r = tmp;
        }
        x-&gt;c = count(x-&gt;l) + count(x-&gt;r) + 1;
    }
}


Insert element.

void insert(Node **t, int k, int v)
{   Node *n = new Node(v);
    Node *left, *right;
    split(*t, k, &amp;left, &amp;right);
    join(t, left, n);
    join(t, *t, right);
}


Remove element.

void remove(Node **t, int k)
{   Node *left, *mid, *tmp, *right;
    split(*t, k, &amp;left, &amp;mid);
    split(mid, k + 1, &amp;tmp, &amp;right);
    join(t, left, right);
}
You should be giving your variables proper names. This would really improve the readability of your code. After all, code is read more times than it is written. As an outsider not all too well versed in advanced data structures, it's really hard to tell what k or v is supposed to be. Your code will effectively be self documenting if you change c and all other one character identifiers to something more reasonable like subtree_size or at least subsize. You won't need comments like /* left and right subtrees */.