A Simple Example: Clearable Stack
ADT ClearableStack
Standard operations:
push(), pop(), isEmpty()
Plus a new operation:
clear() = remove everything
from the Stack
Suppose we implement
clear() by using multiple
pops
What is the worst-case time
for n ClearableStack
operations starting with an
empty Stack?
An analysis:
clear() can take O(n) time in
the worst-case
Thus, worst-case time for n
operations is O(n2)
A better analysis
There is at most one pop()
for each insert()
Thus total time is
proportional to number of
inserts = O(n)
Thus the amortized time per
operation is O(1)
CS409 - Spring 2000
2