0

set::insert results in no changes to iterator validity [cplusplus.com].

The common implementation of std::set is red-black tree. Why are there no changes to iterator validity with reference to RB-tree insertion?

The way I understand RB-tree insertion is to first convert it to a 2,4-tree, do the insert and then convert back. However, from a previous question,

With the B-tree based implementation, due to node splits and consolidations, the erase member functions on these new structures may invalidate iterators to other elements in the tree

halfer
  • 19,824
  • 17
  • 99
  • 186
AustinBest
  • 23
  • 5

1 Answers1

0

Why are there no changes to iterator validity with reference to RB-tree insertion?

Because I think set iterators are implemented as just a pointer to a Node. As you say sets are commonly implemented as a RB-tree.

Now while the parent, left and right pointers and the colour may change, the content of the node is the same. For std::set, std::map, an iterator to the node remains valid until the node is deleted.

RB-tree insertion RB-tree insertion can be done in-situ, without any conversion.

halfer
  • 19,824
  • 17
  • 99
  • 186
SJHowe
  • 756
  • 5
  • 11