Modified Build Tree Function