Excise tax 3-7 in the book "Algorithm Design Guide" says:
Suppose you have access to a balanced dictionary data structure that supports each of the search, insert, delete, minimum, maximum, successor and predecessor operations in O (log n). Explain how to change the insert and delete operations so that they still do O (log n), but now the minimum and maximum time is O (1). (Hint: think in terms of using abstract dictionary operations, rather than resetting pointers, etc. ... )
Without hints, I think this question is pretty simple.
Here is my solution:
for a tree, I just maintain a min pointer that always points to a minimum, and another max pointer always points to a maximum.
When inserting x, I simply compare min.key with x.key, if min.key > x.key, then min = x;
and also do it for max, if necessary.
When removing x, if min==x, then min = successor(x); if max==x, then max = predecessor(x);
if min==x, then min = successor(x); if max==x, then max = predecessor(x);
But the hint says that I can't avoid pointers and the like. Is my decision disgusting with pointers?
If we cannot use extra points, how can I get O (1) for minimum and maximum?
thanks