2.7 Discussion and Exercises

Most of the data structures described in this chapter are folklore. They can be found in implementations dating back over 30 years. For example, implementations of stacks, queues, and deques, which generalize easily to the ArrayStack, ArrayQueue and ArrayDeque structures described here, are discussed by Knuth [46, Section 2.2.2].

Brodnik et al. [13] seem to have been the first to describe the RootishArrayStack and prove a $ \sqrt{n}$ lower-bound like that in Section 2.6.2. They also present a different structure that uses a more sophisticated choice of block sizes in order to avoid computing square roots in the $ \ensuremath{\mathrm{i2b}(\ensuremath{\mathit{i}})}$ method. Within their scheme, the block containing $ \ensuremath{\ensuremath{\mathit{i}}}$ is block $ \lfloor\log (\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}+1)\rfloor$, which is simply the index of the leading 1 bit in the binary representation of $ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}+1$. Some computer architectures provide an instruction for computing the index of the leading 1-bit in an integer.

A structure related to the RootishArrayStack is the two-level tiered-vector of Goodrich and Kloss [35]. This structure supports the $ \ensuremath{\mathrm{get}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{set}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ operations in constant time and $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ in $ O(\sqrt{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}})$ time. These running times are similar to what can be achieved with the more careful implementation of a RootishArrayStack discussed in Exercise 2.10.

Exercise 2..1   The List method $ \ensuremath{\mathrm{add\_all}(\ensuremath{\mathit{i}},\ensuremath{\mathit{c}})}$ inserts all elements of the Collection $ \ensuremath{\ensuremath{\mathit{c}}}$ into the list at position $ \ensuremath{\ensuremath{\mathit{i}}}$. (The $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ method is a special case where $ \ensuremath{\ensuremath{\ensuremath{\mathit{c}}}}=\{\ensuremath{\ensuremath{\ensuremath{\mathit{x}}}}\}$.) Explain why, for the data structures in this chapter, it is not efficient to implement $ \ensuremath{\mathrm{add\_all}(\ensuremath{\mathit{i}},\ensuremath{\mathit{c}})}$ by repeated calls to $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$. Design and implement a more efficient implementation.

Exercise 2..2   Design and implement a RandomQueue. This is an implementation of the Queue interface in which the $ \ensuremath{\mathrm{remove}()}$ operation removes an element that is chosen uniformly at random among all the elements currently in the queue. (Think of a RandomQueue as a bag in which we can add elements or reach in and blindly remove some random element.) The $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}()}$ operations in a RandomQueue should run in constant time per operation.

Exercise 2..3   Design and implement a Treque (triple-ended queue). This is a List implementation in which $ \ensuremath{\mathrm{get}(\ensuremath{\mathit{i}})}$ and $ \ensuremath{\mathrm{set}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ run in constant time and $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ run in time

$\displaystyle O(1+\min\{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}, \ens...
...{n}}}}/2-\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\vert\}) \enspace .
$

In other words, modifications are fast if they are near either end or near the middle of the list.

Exercise 2..4   Implement a method $ \ensuremath{\mathrm{rotate}(\ensuremath{\mathit{a}},\ensuremath{\mathit{r}})}$ that ``rotates'' the array $ \ensuremath{\ensuremath{\mathit{a}}}$ so that $ \ensuremath{\ensuremath{\mathit{a}}[\ensuremath{\mathit{i}}]}$ moves to $ \ensuremath{\ensuremath{\ensuremath{\mathit{a}}}}[(\ensuremath{\ensuremath{\en...
...r}}}})\bmod \ensuremath{\ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})}}]$, for all $ \ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\in\{0,\ldots,\ensuremath{\ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})}}\}$.

Exercise 2..5   Implement a method $ \ensuremath{\mathrm{rotate}(\ensuremath{\mathit{r}})}$ that ``rotates'' a List so that list item $ \ensuremath{\ensuremath{\mathit{i}}}$ becomes list item $ (\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}+\ensuremath{\ensuremath{\ensuremath{\mathit{r}}}})\bmod \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}$. When run on an ArrayDeque, or a DualArrayDeque, $ \ensuremath{\mathrm{rotate}(\ensuremath{\mathit{r}})}$ should run in $ O(1+\min\{\ensuremath{\ensuremath{\ensuremath{\mathit{r}}}},\ensuremath{\ensur...
...{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{r}}}}\})$ time.

Exercise 2..6   This exercise is left out of the pseudocode edition.

Exercise 2..7   Modify the ArrayDeque implementation so that it does not use the $ \bmod $ operator (which is expensive on some systems). Instead, it should make use of the fact that, if $ \ensuremath{\mathrm{length}(\ensuremath{\mathit{a}})}$ is a power of 2, then

$\displaystyle \ensuremath{\ensuremath{\ensuremath{\mathit{k}}\bmod \mathrm{leng...
...{\mathit{k}} \wedge (\mathrm{length}(\ensuremath{\mathit{a}})-1)}} \enspace .
$

(Here, $ \wedge $ is the bitwise-and operator.)

Exercise 2..8   Design and implement a variant of ArrayDeque that does not do any modular arithmetic at all. Instead, all the data sits in a consecutive block, in order, inside an array. When the data overruns the beginning or the end of this array, a modified $ \ensuremath{\mathrm{rebuild}()}$ operation is performed. The amortized cost of all operations should be the same as in an ArrayDeque.

Hint: Getting this to work is really all about how you implement the $ \ensuremath{\mathrm{rebuild}()}$ operation. You would like $ \ensuremath{\mathrm{rebuild}()}$ to put the data structure into a state where the data cannot run off either end until at least $ \ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}/2$ operations have been performed.

Test the performance of your implementation against the ArrayDeque. Optimize your implementation (by using $ \ensuremath{\mathrm{System}.\mathrm{arraycopy}(\ensuremath{\mathit{a}},\ensure...
...t{i}},\ensuremath{\mathit{b}},\ensuremath{\mathit{i}},\ensuremath{\mathit{n}})}$) and see if you can get it to outperform the ArrayDeque implementation.

Exercise 2..9   Design and implement a version of a RootishArrayStack that has only $ O(\sqrt{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}})$ wasted space, but that can perform $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ operations in $ O(1+\min\{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}},\ensuremath{\ensur...
...{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\})$ time.

Exercise 2..10   Design and implement a version of a RootishArrayStack that has only $ O(\sqrt{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}})$ wasted space, but that can perform $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ operations in $ O(1+\min\{\sqrt{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}},\ensuremath...
...{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\})$ time. (For an idea on how to do this, see Section 3.3.)

Exercise 2..11   Design and implement a version of a RootishArrayStack that has only $ O(\sqrt{\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}})$ wasted space, but that can perform $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ operations in $ O(1+\min\{\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}},\sqrt {\ensuremath...
...{\ensuremath{\mathit{n}}}}-\ensuremath{\ensuremath{\ensuremath{\mathit{i}}}}\})$ time. (See Section 3.3 for ideas on how to achieve this.)

Exercise 2..12   Design and implement a CubishArrayStack. This three level structure implements the List interface using $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}^{2/3})$ wasted space. In this structure, $ \ensuremath{\mathrm{get}(\ensuremath{\mathit{i}})}$ and $ \ensuremath{\mathrm{set}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ take constant time; while $ \ensuremath{\mathrm{add}(\ensuremath{\mathit{i}},\ensuremath{\mathit{x}})}$ and $ \ensuremath{\mathrm{remove}(\ensuremath{\mathit{i}})}$ take $ O(\ensuremath{\ensuremath{\ensuremath{\mathit{n}}}}^{1/3})$ amortized time.

opendatastructures.org