Recursive traversal processes a node, then recursively visits each child. It works naturally for trees and directed acyclic graphs, but cycles can repeat the same state until stack space is exhausted. section 5.4.2
Figure 5.7 Finite recursion (left) and infinite recursion (right)
Contrast finite recursion with infinite recursion caused by revisiting equivalent program states. section 5.4.2
Avoid traversal failures by distinguishing valid structure assumptions from traversal defects:
- Track previously visited nodes when cycles are legal or possible. section 5.4.2
- Fix the earlier data-structure mistake when cycles should be impossible; do not obscure a valid traversal with unnecessary protection. section 5.4.2
Self-referential collections
A list containing itself can make list hashCode() recurse until StackOverflowError; toString() protects against a direct self-cycle, but nested cycles remain problematic. section 5.4.2
