implemeted build_tree