implemented max_depth algorithm