0

See https://en.cppreference.com/w/cpp/container/map/erase for reference.

How is erasing a single iterator able to be done in amortized constant time? This seems to imply that the rebalancing of the red black tree can be done in constant time.

anonuser01
  • 1,307
  • 7
  • 14
  • It might not be balanced in constant time, but it can be balanced in _amortized_ constant time – Mooing Duck Aug 24 '20 at 16:51
  • Amortized constant time doesn't actually mean it could be done in constant time, rather there are certain part of the action was done in constant (`erase`), but other parts are not (rebalancing). For more, refer to: https://stackoverflow.com/questions/200384/constant-amortized-time – Ranoiaetep Aug 24 '20 at 18:43

1 Answers1

1

Amortized constant time doesn't mean that any single operation takes constant time, but that, over many such operations, the non-constant overhead of some operations amortizes to overall constant time.

In other words, if you use erase repeatedly to clear the entire map, that loop finishes in linear time in the number of elements, and thus constant time per element.

For a more thorough analysis, cs.stackexchange.com would probably be a more appropriate place.

Sebastian Redl
  • 61,331
  • 8
  • 105
  • 140