Fast and optimal prediction of a labeled tree