added ancestors in tree maker and changed optimizeTree for multi-level trees