add tree iterator
sort and dynamic programming