tree building and prediction logic