implemented non-recursive tree walking and building