[ForeachTreeBuilder] Foreach tree builder implementation and tests