You have a binary tree and an inorder iterator implementation that maintains internal traversal state (e.g., a stack). Consider a multi-threaded program where:
-
Multiple threads may create and use their own iterators over the
same tree
concurrently.
-
The tree may be either
read-only
during iteration, or it may be
mutated
concurrently (inserts/deletes/rotations).
Question
How would you design the iterator and surrounding system to be thread-safe under these scenarios?
In your answer, cover:
-
What is safe when the tree is read-only?
-
What can go wrong when the tree is mutated concurrently?
-
At least two approaches to handle concurrent mutations (e.g., locking, snapshotting/versioning), including trade-offs.