The XFastTrie is a big improvement over the BinaryTrie in terms of query time--some would even call it an exponential improvement--but the $ \mathtt{add(x)}$ and $ \mathtt{remove(x)}$ operations are still not terribly fast. Furthermore, the space usage, $ O(\ensuremath{\mathtt{n}}\cdot\ensuremath{\mathtt{w}})$, is higher than the other SSet implementation in this book, which all use $ O(\ensuremath{\mathtt{n}})$ space. These two problems are related; if $ \mathtt{n}$ $ \mathtt{add(x)}$ operations build a structure of size $ \ensuremath{\mathtt{n}}\cdot\ensuremath{\mathtt{w}}$ then the $ \mathtt{add(x)}$ operation requires on the order of $ \mathtt{w}$ time (and space) per operation.
The YFastTrie data structure simultaneously addresses both the space and speed issues of XFastTries. A YFastTrie uses an XFastTrie, $ \mathtt{xft}$, but only stores $ O(\ensuremath{\mathtt{n}}/\ensuremath{\mathtt{w}})$ values in $ \mathtt{xft}$. In this way, the total space used by $ \mathtt{xft}$ is only $ O(\ensuremath{\mathtt{n}})$. Furthermore, only one out of every $ \mathtt{w}$ $ \mathtt{add(x)}$ or $ \mathtt{remove(x)}$ operations in the YFastTrie results in an $ \mathtt{add(x)}$ or $ \mathtt{remove(x)}$ operation in $ \mathtt{xft}$. By doing this, the average cost incurred by calls to $ \mathtt{xft}$'s $ \mathtt{add(x)}$ and $ \mathtt{remove(x)}$ operations is only constant.
The obvious question becomes: If $ \mathtt{xft}$ only stores $ \mathtt{n}$/ $ \mathtt{w}$ elements, where do the remaining $ \ensuremath{\mathtt{n}}(1-1/\ensuremath{\mathtt{w}})$ elements go? These elements go into secondary structures, in this case an extended version of treaps (). There are roughly $ \mathtt{n}$/ $ \mathtt{w}$ of these secondary structures so, on average, each of them stores $ O(\ensuremath{\mathtt{w}})$ items. Treaps support logarithmic time SSet operations, so the operations on these treaps will run in $ O(\log \ensuremath{\mathtt{w}})$ time, as required.
More concretely, a YFastTrie contains an XFastTrie, $ \mathtt{xft}$, that contains a random sample of the data, where each element appears in the sample independently with probability $ 1/\ensuremath{\mathtt{w}}$. For convenience, the value $ 2^{\ensuremath{\mathtt{w}}}-1$, is always contained in $ \mathtt{xft}$. Let $ \ensuremath{\mathtt{x}}_0<\ensuremath{\mathtt{x}}_1<\cdots<\ensuremath{\mathtt{x}}_{k-1}$ denote the elements stored in $ \mathtt{xft}$. Associated with each element, $ \ensuremath{\mathtt{x}}_i$, is a treap, $ \ensuremath{\mathtt{t}}_i$, that stores all values in the range $ \ensuremath{\mathtt{x}}_{i-1}+1,\ldots,\ensuremath{\mathtt{x}}_i$. This is illustrated in Figure 13.7.
The $ \mathtt{find(x)}$ operation in a YFastTrie is fairly easy. We search for $ \mathtt{x}$ in $ \mathtt{xft}$ and find some value $ \ensuremath{\mathtt{x}}_i$ associated with the treap $ \ensuremath{\mathtt{t}}_i$. When then use the treap $ \mathtt{find(x)}$ method on $ \ensuremath{\mathtt{t}}_i$ to answer the query. The entire method is a one-liner:
T find(T x) {
return xft.find(new Pair<T>(it.intValue(x))).t.find(x);
}
The first
$ \mathtt{find(x)}$ operation (on
$ \mathtt{xft}$) takes
$ O(\log \ensuremath{\mathtt{w}})$ time.
The second
$ \mathtt{find(x)}$ operation (on a treap) takes $ O(\log r)$ time, where
$ r$ is the size of the treap. Later in this section, we will show that
the expected size of the treap is
$ O(\ensuremath{\mathtt{w}})$ so that this operation takes
$ O(\log \ensuremath{\mathtt{w}})$ time.1
Adding an element to a YFastTrie is also fairly simple--most of the time. The $ \mathtt{add(x)}$ method calls $ \mathtt{xft.find(x)}$ to locate the treap, $ \mathtt{t}$, into which $ \mathtt{x}$ should be inserted. It then calls $ \mathtt{t.add(x)}$ to add $ \mathtt{x}$ to $ \mathtt{t}$. At this point, it tosses a biased coin, that comes up heads with probability $ 1/\ensuremath{\mathtt{w}}$. If this coin comes up heads, $ \mathtt{x}$ will be added to $ \mathtt{xft}$.
This is where things get a little more complicated. When $ \mathtt{x}$ is added to $ \mathtt{xft}$, the treap $ \mathtt{t}$ needs to be split into two treaps $ \mathtt{t1}$ and $ \mathtt{t'}$. The treap $ \mathtt{t1}$ contains all the values less than or equal to $ \mathtt{x}$; $ \mathtt{t'}$ is the original treap, $ \mathtt{t}$, with the elements of $ \mathtt{t1}$ removed. Once this is done we add the pair $ \mathtt{(x,t1)}$ to $ \mathtt{xft}$. Figure 13.8 shows an example.
boolean add(T x) {
int ix = it.intValue(x);
STreap<T> t = xft.find(new Pair<T>(ix)).t;
if (t.add(x)) {
n++;
if (rand.nextInt(w) == 0) {
STreap<T> t1 = t.split(x);
xft.add(new Pair<T>(ix, t1));
}
return true;
}
return false;
}
The $ \mathtt{remove(x)}$ method just undoes the work performed by $ \mathtt{add(x)}$. We use $ \mathtt{xft}$ to find the leaf, $ \mathtt{u}$, in $ \mathtt{xft}$ that contains the answer to $ \mathtt{xft.find(x)}$. From $ \mathtt{u}$, we get the treap, $ \mathtt{t}$, containing $ \mathtt{x}$ and remove $ \mathtt{x}$ from $ \mathtt{t}$. If $ \mathtt{x}$ was also stored in $ \mathtt{xft}$ (and $ \mathtt{x}$ is not equal to $ 2^{\ensuremath{\mathtt{w}}}-1$) then we remove $ \mathtt{x}$ from $ \mathtt{xft}$ and add the elements from $ \mathtt{x}$'s treap to the treap, $ \mathtt{t2}$, that is stored by $ \mathtt{u}$'s successor in the linked list. This is illustrated in Figure 13.9.
boolean remove(T x) {
int ix = it.intValue(x);
Node<T> u = xft.findNode(ix);
boolean ret = u.x.t.remove(x);
if (ret) n--;
if (u.x.x == ix && ix != 0xffffffff) {
STreap<T> t2 = u.child[1].x.t;
t2.absorb(u.x.t);
xft.remove(u.x);
}
return ret;
}
Earlier in the discussion, we put off arguing about the sizes of treaps in this structure until later. Before finishing we prove the result we need.
Similarly, the elements of $ \mathtt{t}$ smaller than $ \mathtt{x}$ are $ \ensuremath{\mathtt{x}}_{i-1},\ldots,\ensuremath{\mathtt{x}}_{i-k}$ where all these $ k$ coin tosses come up tails and the coin toss for $ \ensuremath{\mathtt{x}}_{i-k-1}$ comes up heads. Therefore, $ \mathrm{E}[k]\le\ensuremath{\mathtt{w}}-1$, since this is the same coin tossing experiment considered previously, but in which the last toss doesn't count. In summary, $ \ensuremath{\mathtt{n}}_\ensuremath{\mathtt{x}}=j+k$, so
Lemma 13.1 was the last piece in the proof of the following theorem, which summarizes the performance of the YFastTrie:
The $ \mathtt{w}$ term in the space requirement comes from the fact that $ \mathtt{xft}$ always stores the value $ 2^\ensuremath{\mathtt{w}}-1$. The implementation could be modified (at the expense of adding some extra cases to the code) so that it is unnecessary to store this value. In this case, the space requirement in the theorem becomes $ O(\ensuremath{\mathtt{n}})$.