RootishArrayStack Flashcards
What is the runtime of get(i) for a RootishArrayStack?
O(1)
What is the runtime of set(i,x) for a RootishArrayStack?
O(1)
What is the runtime of add(i,x) for a RootishArrayStack?
O(n-i) amortized
What is the runtime of remove(i) for a RootishArrayStack?
O(n-i) amortized
What is the extra space required for a RootishArrayStack to store n elements?
O(sqrt(n))
What is the backing storage in a RootishArrayStack?
O(sqrt(n)) arrays of sizes 1, 2, 3, 4, ….
The first r blocks of a RootishArrayStack can store up to how many elements?
r(r+1)/2.
The first r blocks have capacity 1, 2, 3, 4, …, r and 1+2+3+…+r = r(r+1)/2
What index i yields the worst-case running time for the add(i,x) operation in a RootishArrayStack?
index i=0.
Adding to the first position requires all remaining n elements to be shifted to the right, which is O(n) work.
What index i yields the best-case running time for the add(i,x) operation in a RootishArrayStack?
index i=n-1.
I.e. adding to the end is fast since it doesn’t require much shifting. You may need to add a new block, but allocating a new block does not take much time in most systems.
Pros of RootishArrayStacks.
Use only O(sqrt(n)) extra space to store n elements. Fast get(i) and set(i,x) (i.e. good random access) Its runtimes are not even amortized, unless array allocation is not constant time (and if it isn't constant, it's still good amortized time.)
Cons of RootishArrayStacks
Slightly more complex implementation than some other array-based implementations of the list interface.
Adding and removing from the front is O(n), so this is not a good choice to implement the Deque interface.