Bird
Raised Fist0

If you use a depth-first iterator to traverse this composite, what will be the sequence of visited nodes?

easy➡️ Traversal Q3 of Q15
OOP & Design Patterns - Iterator & Composite Pattern - Traversal Abstractions
Consider a Composite structure where a composite node contains two leaf nodes labeled 'X' and 'Y'. If you use a depth-first iterator to traverse this composite, what will be the sequence of visited nodes?
AComposite, X, Y
BX, Y, Composite
CY, X, Composite
DComposite, Y, X
Step-by-Step Solution
Solution:
  1. Step 1: Understand traversal order

    Depth-first traversal visits the composite node first, then its children.
  2. Step 2: Apply to given structure

    The composite is visited first, followed by leaf 'X', then leaf 'Y'.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Traversal order matches depth-first pre-order [OK]
Quick Trick: Depth-first pre-order visits parent before children [OK]
Common Mistakes:
MISTAKES
  • Assuming leaves are visited before the composite
  • Confusing depth-first with breadth-first traversal
Trap Explanation:
PITFALL
  • Thinking traversal visits leaves before the composite node
Interviewer Note:
CONTEXT
  • Tests understanding of traversal order in composite iterators
Master "Iterator & Composite Pattern - Traversal Abstractions" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes