Analysis of RootishArrayStack
Explore the advantage of using RootishArrayStack.
We'll cover the following...
Growing and shrinking
Note that, unlike the ArrayStack.resize()
operation, grow()
and shrink()
do not copy any data. They only allocate or free an array of size . In some environments, this takes only constant time, while in others, it may require time proportional to .
We note that, immediately after a call to grow()
or shrink()
, the situation is clear. The final block is completely empty, and all other blocks are completely full. Another call to grow()
or shrink()
will not happen until at least elements have been added or removed. Therefore, even if grow()
and shrink()
take time, this cost can be amortized over at least add(i, x)
or remove(i)
operations, so that the amortized cost of grow()
and shrink()
is per operation.
Space usage
Next, we analyze the amount of extra space used by a RootishArrayStack
. In particular, we want to count any space used by a RootishArrayStack
that is not an array element currently used to hold a list element. We call all such space wasted space.
The remove(i)
operation ensures that a RootishArrayStack
never has more than two blocks that are not completely full. The number of blocks, , used by a RootishArrayStack
that stores elements therefore satisfies
Again, using the quadratic equation on this gives
The last two blocks have sizes and , so the space wasted by these two blocks is at most ...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy