ast algorithm