Generic tree walk implementation