Johs wrote:
I have implemented a red-black tree based on the description in Introduction
to Algorithms Cormen section 13. But I would like to make all iterator
operations to take O(1) time in worst case. Is this even possible and if so
does anyone know of any articles websites dealing with this optimzation?
I suppose you could have two additional pointers in the tree nodes:
One pointer for the previous node and another for the next node (in the
traversal order). You'll have to figure out how to update these pointers
when nodes are added and removed from the tree, but I'm confident that
it can be done in log n time.
OTOH one could ask if you really need this. While one single iterator
increment or decrement may require O(log n) steps, the total number of
steps for a full traversal is still O(n) (not even amortized O(n), but
pure O(n)). So you should ask yourself if random single increments /
decrements are so frequent in your program that it justifies the
additional memory needed for the pointers and the overhead of keeping
them updated when the tree changes.