fixed max and min depth algorithm