8.2 Discussion and Exercises

The term scapegoat tree is due to Galperin and Rivest [33], who define and analyze these trees. However, the same structure was discovered earlier by Andersson [5,7], who called them general balanced trees since they can have any shape as long as their height is small.

Experimenting with the ScapegoatTree implementation will reveal that it is often considerably slower than the other SSet implementations in this book. This may be somewhat surprising, since height bound of

$\displaystyle \log_{3/2}\ensuremath{\ensuremath{\ensuremath{\mathit{q}}}} \approx 1.709\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}} + O(1)
$

is better than the expected length of a search path in a Skiplist and not too far from that of a Treap. The implementation could be optimized by storing the sizes of subtrees explicitly at each node or by reusing already computed subtree sizes (Exercises 8.5 and 8.6). Even with these optimizations, there will always be sequences of $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{delete}(\ensuremath{\mathit{x}})}$ operation for which a ScapegoatTree takes longer than other SSet implementations.

This gap in performance is due to the fact that, unlike the other SSet implementations discussed in this book, a ScapegoatTree can spend a lot of time restructuring itself. Exercise 8.3 asks you to prove that there are sequences of $ \ensuremath{\ensuremath{\mathit{n}}}$ operations in which a ScapegoatTree will spend on the order of $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}$ time in calls to $ \ensuremath{\mathrm{rebuild}(\ensuremath{\mathit{u}})}$. This is in contrast to other SSet implementations discussed in this book, which only make $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ structural changes during a sequence of $ \ensuremath{\ensuremath{\mathit{n}}}$ operations. This is, unfortunately, a necessary consequence of the fact that a ScapegoatTree does all its restructuring by calls to $ \ensuremath{\mathrm{rebuild}(\ensuremath{\mathit{u}})}$ [20].

Despite their lack of performance, there are applications in which a ScapegoatTree could be the right choice. This would occur any time there is additional data associated with nodes that cannot be updated in constant time when a rotation is performed, but that can be updated during a $ \ensuremath{\mathrm{rebuild}(\ensuremath{\mathit{u}})}$ operation. In such cases, the ScapegoatTree and related structures based on partial rebuilding may work. An example of such an application is outlined in Exercise 8.11.

Exercise 8..1   Illustrate the addition of the values 1.5 and then 1.6 on the ScapegoatTree in Figure 8.1.

Exercise 8..2   Illustrate what happens when the sequence $ 1,5,2,4,3$ is added to an empty ScapegoatTree, and show where the credits described in the proof of Lemma 8.3 go, and how they are used during this sequence of additions.

Exercise 8..3   Show that, if we start with an empty ScapegoatTree and call $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ for $ \ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}=1,2,3,\ldots,\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}$, then the total time spent during calls to $ \ensuremath{\mathrm{rebuild}(\ensuremath{\mathit{u}})}$ is at least $ c\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}$ for some constant $ c>0$.

Exercise 8..4   The ScapegoatTree, as described in this chapter, guarantees that the length of the search path does not exceed $ \log_{3/2}\ensuremath{\ensuremath{\ensuremath{\mathit{q}}}}$.
  1. Design, analyze, and implement a modified version of ScapegoatTree where the length of the search path does not exceed $ \log_{\ensuremath{\ensuremath{\ensuremath{\mathit{b}}}}} \ensuremath{\ensuremath{\ensuremath{\mathit{q}}}}$, where $ \ensuremath{\ensuremath{\mathit{b}}}$ is a parameter with $ 1<\ensuremath{\ensuremath{\ensuremath{\mathit{b}}}}<2$.
  2. What does your analysis and/or your experiments say about the amortized cost of $ \ensuremath{\mathrm{find}(\ensuremath{\mathit{x}})}$, $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ as a function of $ \ensuremath{\ensuremath{\mathit{n}}}$ and $ \ensuremath{\ensuremath{\mathit{b}}}$?

Exercise 8..5   Modify the $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ method of the ScapegoatTree so that it does not waste any time recomputing the sizes of subtrees that have already been computed. This is possible because, by the time the method wants to compute $ \ensuremath{\mathrm{size}(\ensuremath{\mathit{w}})}$, it has already computed one of $ \ensuremath{\mathrm{size}(\ensuremath{\mathit{w}}.\ensuremath{\mathit{left}})}$ or $ \ensuremath{\mathrm{size}(\ensuremath{\mathit{w}}.\ensuremath{\mathit{right}})}$. Compare the performance of your modified implementation with the implementation given here.

Exercise 8..6   Implement a second version of the ScapegoatTree data structure that explicitly stores and maintains the sizes of the subtree rooted at each node. Compare the performance of the resulting implementation with that of the original ScapegoatTree implementation as well as the implementation from Exercise 8.5.

Exercise 8..7   Reimplement the $ \ensuremath{\mathrm{rebuild}(\ensuremath{\mathit{u}})}$ method discussed at the beginning of this chapter so that it does not require the use of an array to store the nodes of the subtree being rebuilt. Instead, it should use recursion to first connect the nodes into a linked list and then convert this linked list into a perfectly balanced binary tree. (There are very elegant recursive implementations of both steps.)

Exercise 8..8   Analyze and implement a WeightBalancedTree. This is a tree in which each node $ \ensuremath{\ensuremath{\mathit{u}}}$, except the root, maintains the balance invariant that $ \ensuremath{\ensuremath{\mathrm{size}(\ensuremath{\mathit{u}})}} \le (2/3)\ens...
...nsuremath{\mathrm{size}(\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}})}}$. The $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations are identical to the standard BinarySearchTree operations, except that any time the balance invariant is violated at a node $ \ensuremath{\ensuremath{\mathit{u}}}$, the subtree rooted at $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{parent}}}$ is rebuilt. Your analysis should show that operations on a WeightBalancedTree run in $ O(\log\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ amortized time.

Exercise 8..9   Analyze and implement a CountdownTree. In a CountdownTree each node $ \ensuremath{\ensuremath{\mathit{u}}}$ keeps a timer $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{t}}}$. The $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations are exactly the same as in a standard BinarySearchTree except that, whenever one of these operations affects $ \ensuremath{\ensuremath{\mathit{u}}}$'s subtree, $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{t}}}$ is decremented. When $ \ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{t}}}}=0$ the entire subtree rooted at $ \ensuremath{\ensuremath{\mathit{u}}}$ is rebuilt into a perfectly balanced binary search tree. When a node $ \ensuremath{\ensuremath{\mathit{u}}}$ is involved in a rebuilding operation (either because $ \ensuremath{\ensuremath{\mathit{u}}}$ is rebuilt or one of $ \ensuremath{\ensuremath{\mathit{u}}}$'s ancestors is rebuilt) $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{t}}}$ is reset to $ \ensuremath{\ensuremath{\mathrm{size}(\ensuremath{\mathit{u}})}}/3$.

Your analysis should show that operations on a CountdownTree run in $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ amortized time. (Hint: First show that each node $ \ensuremath{\ensuremath{\mathit{u}}}$ satisfies some version of a balance invariant.)

Exercise 8..10   Analyze and implement a DynamiteTree. In a DynamiteTree each node $ \ensuremath{\ensuremath{\mathit{u}}}$ keeps tracks of the size of the subtree rooted at $ \ensuremath{\ensuremath{\mathit{u}}}$ in a variable $ \ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{size}}}$. The $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{x}})}$ operations are exactly the same as in a standard BinarySearchTree except that, whenever one of these operations affects a node $ \ensuremath{\ensuremath{\mathit{u}}}$'s subtree, $ \ensuremath{\ensuremath{\mathit{u}}}$ explodes with probability $ 1/\ensuremath{\ensuremath{\ensuremath{\mathit{u}}.\ensuremath{\mathit{size}}}}$. When $ \ensuremath{\ensuremath{\mathit{u}}}$ explodes, its entire subtree is rebuilt into a perfectly balanced binary search tree.

Your analysis should show that operations on a DynamiteTree run in $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ expected time.

Exercise 8..11   Design and implement a Sequence data structure that maintains a sequence (list) of elements. It supports these operations: The first two operations should run in $ O(\log \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}})$ amortized time. The third operation should run in constant time.

The Sequence data structure can be implemented by storing the elements in something like a ScapegoatTree, in the same order that they occur in the sequence. To implement $ \ensuremath{\mathrm{test\_before}(\ensuremath{\mathit{e_1}},\ensuremath{\mathit{e_2}})}$ in constant time, each element $ \ensuremath{\ensuremath{\mathit{e}}}$ is labelled with an integer that encodes the path from the root to $ \ensuremath{\ensuremath{\mathit{e}}}$. In this way, $ \ensuremath{\mathrm{test\_before}(\ensuremath{\mathit{e_1}},\ensuremath{\mathit{e_2}})}$ can be implemented by comparing the labels of $ \ensuremath{\ensuremath{\mathit{e_1}}}$ and $ \ensuremath{\ensuremath{\mathit{e_2}}}$.

opendatastructures.org