std::set min/max element access (through begin/rbegin) is constant time. Does it achieve this by updating min/max during insertion/removal?
Asked
Active
Viewed 281 times
2
-
A set is always sorted, so the minimal and maximal values are always at the beginning and the end. – tkausl Jan 23 '20 at 22:25
-
@tkausl when are beginning and end updated? – qwr Jan 23 '20 at 22:26
-
It's not hard to implement a set such that the root _is_ the "end", so then you only have to keep track of the beginning. I assume they simply have 'begin' (maybe and 'end') as additional members that they update when updating the data. Do you think this would be hard in some way? – Mooing Duck Jan 23 '20 at 22:26
-
@MooingDuck in red-black tree couldn't min and max be anywhere? For balance purposes it does not make sense to have either extreme as root. – qwr Jan 23 '20 at 22:28
-
@tkausl Whats the beginning and end of a RBTree? OP: *Does it achieve this by updating min/max during insertion/removal?* That's really the only way to do it. – NathanOliver Jan 23 '20 at 22:32
-
1@qwr: No. In any ordered tree, the 'begin' is the leftmost leaf node, and the 'end' is 'one past the rightmost leaf node', regardless of implementation, redblack or otherwise – Mooing Duck Jan 23 '20 at 22:49