remove recursion from TreeMap implementation