Design an Extensible Tree Traversal Toolkit
Design an in-process traversal library with pluggable strategies, visitors, lazy iteration, cancellation, and bounded parallelism.
Problem statement
Design an in-process toolkit that supports preorder, inorder, postorder, and level-order traversal strategies while separating traversal mechanics from caller-supplied visitor behavior. A visitor result may request early termination. Expose lazy iterator traversal for trees too large to materialize eagerly.
Permit parallel subtree work only under an explicit thread-safe visitor contract.
For this exercise, assume read-only rooted trees with up to ten million nodes, stable child order, bounded caller cancellation, no recursion requirement, and a memory target proportional to traversal frontier depth or breadth rather than total node count.
Define public contracts, ownership, ordering, lifecycle, error propagation, resource bounds, extension points, and tests without choosing a programming language.
What to build
- A caller selects preorder, inorder, postorder, or level-order traversal without changing node or visitor implementations.
- A caller supplies visitor behavior whose typed result can continue, stop successfully, or fail with an explicit error.
- A caller can pull nodes lazily, cancel, and release traversal resources without materializing the full tree.
- A caller may opt into bounded parallel subtree work only with a declared thread-safe visitor and documented ordering semantics.
At scale
- Source-backed behavior: four traversal strategies are independent from visitor behavior, and visitors can request early termination.
- Source-backed behavior: lazy iteration supports large trees, while parallel traversal requires a documented thread-safe visitor.
- Practice assumption: input is a read-only rooted tree with stable child order and up to ten million nodes.
- Practice assumption: sequential traversal preserves its canonical order; parallel mode guarantees bounded work and cancellation but not one global visitation order unless explicitly serialized.