Various implementations of Balanced Binary Search Tree (BST) guaranteed membership queries in O(logn) time, where n is the number of elements in some set S. The working set property is a stronger requirement than O(logn) time complexity. Suppose that an item x∈S is accessed t queries before, then we want to perform find(x) in O(logt) time.
The idea is to maintain a sequence of Balanced BST's, denoted as T0,T1,T2,.... Denote the size of Ti for some i≥0 as ni. Then the i-th tree maintains the latest ni accessed items. The sizes of ni's satisfy
The largest tree has an index that equals to the smallest k with 2k−1≥logn (assuming that n≥1). It implies that k=O(loglogn). The complexities for searching an element on these trees are
When a query find(x) is invoked, we first search it on T0. If we find it successfully, we stop. Otherwise we search it on T1 and so on. If x is accessed t queries before (and is not accessed in the latest t−1 queries if t≥1), then it is contained in the tree Tix, where
ix≐ the smallest non-negative integer, s.t.2ix−1≥t
As a sanity check, note that when t=0 (x is the latest accessed item), we have ix=0 as 2−1≥0. By definition of definition of ix, x is not contained in any Ti with i<ix. The time to search x is
Finally, we need to update the trees T0, T1, ... Tix to reflect the last access of x. In particular, we 1) insert x into T0, T1, ... Tix−1; 2) for each i<ix, we delete the least recently accessed item; 3) update the access time of x in Tix.
Step 1) takes O(logt) time. To implement steps 2) and 3) in O(logt) time, we create a min-heap Hi for each tree Ti that keeps the the access time of items in Ti. Both fetching least recently accessed item and updating access time can be performed in O(logni) time. Therefore, steps 2) and 3) take O(logt) time in total.